std::experi...ue_resource(3)
π NAME
std::experimental::unique_resource β bestie, it's literally the universal RAII wrapper β¨ (std::experimental::unique_resource, in the C++ Standard Library manual, no cap)
β¨ SYNOPSIS
Declared right here, in header <experimental/scope>:
Defined in header <experimental/scope>
template< class R, class D > (library fundamentals TS v3)
class unique_resource;
Okay so unique_resource is a universal RAII wrapper for resource handles π«Ά β it owns and manages a resource through a handle, and it disposes of that resource when the unique_resource gets destroyed. Main character energy: it holds the thing, then it lets the thing go. Periodt.
The resource gets disposed of using the deleter of type D whenever either of these happens:
- the managing
unique_resourceobject is destroyed π - the managing
unique_resourceobject is assigned from another resource viaoperator=orreset()π
Now, let type RS be R if R is an object type, or std::reference_wrapper<std::remove_reference_t<R>> otherwise:
unique_resourceeffectively holds a subobject of typeRSwhich is or wraps the resource handle, a deleter of typeD, and aboolflag indicating whether the wrapper is owning the resource π- For explanatory purposes only, that subobject of type
RSis called the stored resource handle, and the stored (ifRis an object type) or wrapped (ifRis a reference type)Ris called the underlying resource handle. These two terms are not used by the LFTS π (yes, really β they're just so we can all talk about it without vibing into confusion).
π TEMPLATE PARAMETERS
Rβ resource handle type πΈDβ deleter type π¦
π TYPE REQUIREMENTS
Rshall be an object type or an lvalue reference to an object type. LetUnrefRbestd::remove_reference_t<R>;UnrefRshall beMoveConstructible, and ifUnrefRis notCopyConstructible,std::is_nothrow_move_constructible_v<UnrefR>shall betrue. β οΈ Not a suggestion, bestie β this is a shall.Dshall be aDestructibleandMoveConstructibleFunctionObjecttype, and ifDis notCopyConstructible,std::is_nothrow_move_constructible_v<D>shall betrue. Given an lvaluedof typeDand an lvaluerof typeUnrefR, the expressiond(r)shall be well-formed. π₯
π MEMBER FUNCTIONS
constructorβ constructs a newunique_resource(public member function) β¨destructorβ disposes the managed resource if such is present (public member function) πoperator=β assigns aunique_resource(public member function) π
πΈ MODIFIERS
releaseβ releases the ownership (public member function) ποΈresetβ disposes or replaces the managed resource (public member function) π§Ή
β OBSERVERS
getβ accesses the underlying resource handle (public member function) πget_deleterβ accesses the deleter used for disposing of the managed resource (public member function) πoperator*,operator->β accesses the pointee if the resource handle is a pointer (public member function) π―
π NON-MEMBER FUNCTIONS
make_unique_resource_checkedβ creates aunique_resource, checking invalid value (function template) β
π Deduction guides
(yep, they exist β that's the whole entry, bestie π)
π₯ NOTES
Resource handle types satisfying NullablePointer can also be managed by std::unique_ptr. But β and this is the slay part β unlike unique_ptr, unique_resource does not require NullablePointer. π
It's giving flexible.
π EXAMPLE
This section is incomplete. Reason: no example π
π SEE ALSO
unique_ptrβ smart pointer with unique object ownership semantics (C++11) (class template) π
Category: Todo no example π«
and that's the whole page, bestie π straight from cppreference.com, dated 2024.06.10 β go forth and manage your resources responsibly π§β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms