std::experi...ue_resource(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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_resource object is destroyed πŸ’€
  • the managing unique_resource object is assigned from another resource via operator= or reset() πŸ”

Now, let type RS be R if R is an object type, or std::reference_wrapper<std::remove_reference_t<R>> otherwise:

  • unique_resource effectively holds a subobject of type RS which is or wraps the resource handle, a deleter of type D, and a bool flag indicating whether the wrapper is owning the resource πŸŽ€
  • For explanatory purposes only, that subobject of type RS is called the stored resource handle, and the stored (if R is an object type) or wrapped (if R is a reference type) R is 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

  • R shall be an object type or an lvalue reference to an object type. Let UnrefR be std::remove_reference_t<R>; UnrefR shall be MoveConstructible, and if UnrefR is not CopyConstructible, std::is_nothrow_move_constructible_v<UnrefR> shall be true. ⚠️ Not a suggestion, bestie β€” this is a shall.
  • D shall be a Destructible and MoveConstructible FunctionObject type, and if D is not CopyConstructible, std::is_nothrow_move_constructible_v<D> shall be true. Given an lvalue d of type D and an lvalue r of type UnrefR, the expression d(r) shall be well-formed. πŸ”₯

πŸ‘‘ MEMBER FUNCTIONS

  • constructor β€” constructs a new unique_resource (public member function) ✨
  • destructor β€” disposes the managed resource if such is present (public member function) πŸ’€
  • operator= β€” assigns a unique_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 a unique_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