std::experi...que_resource(3)

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

🌸 NAME 🌸

Okay so bestie, this is the std::experimental::unique_resource::unique_resource page from the C++ Standard Libary manual β€” yes, that’s the cppreference one, and yes, the title is literally std::experimental::unique_resource::unique_resource twice because we love a dramatic repetition. πŸ’… It’s giving RAII royalty, no cap.


✨ Synopsis ✨

Here’s the whole constructor family, looking iconic:

unique_resource();                                  (1) (library
fundamentals TS v3)
template< class RR, class DD >
unique_resource( RR&& r, DD&& d ) noexcept(/*see    (2) (library
fundamentals TS v3)
below*/)
unique_resource( unique_resource&& other );         (3) (library
fundamentals TS v3)

These follow items are used for explanatory purpose, bestie β€” lil shorthand names so we don’t lose our minds:

  • RS is the type of stored resource handle.
  • The expression res_ refers the underlying resource handle.
  • del_ refers the deleter object.

πŸ’– Overload (1) β€” default constructor

Default constructor. Value-initializes the stored resource handle and the deleter. The constructed unique_resource does not own the resource.

This overload participates in overload resolution only if both std::is_default_constructible_v<R> and std::is_default_constructible_v<D> are true.

πŸ¦‹ Overload (2) β€” the ( RR&& r, DD&& d ) one

The stored resource handle is initialized with std::forward<RR>(r) if std::is_nothrow_constructible_v<RS, RR> is true, otherwise r. If initialization of the stored resource handle throws an exception, calls d(r).

Then, the deleter is initialized with std::forward<DD>(d) if std::is_nothrow_constructible_v<D, DD> is true, otherwise d. If initialization of deleter throws an exception, calls d(res_).

The constructed unique_resource owns the resource.

This overload participates in overload resolution only if all of these are true:

  • std::is_constructible_v<RS, RR>
  • std::is_constructible_v<D, DD>
  • std::is_nothrow_constructible_v<RS, RR> || std::is_constructible_v<RS, RR&>
  • std::is_nothrow_constructible_v<D, DD> || std::is_constructible_v<D, DD&>

The program is ill-formed if any of the expressions d(r), d(res_) and del_(res_) is ill-formed.

🚨 Bestie, this is UB territory: the behavior is undefined if any of the expressions d(r), d(res_) and del_(res_) results in undefined behavior or throws an exception.

πŸŽ€ Overload (3) β€” move constructor

Move constructor. The stored resource handle is initialized from the one of other, using std::move if std::is_nothrow_move_constructible_v<RS> is true. If initialization of the stored resource handle throws an exception, other is not modified.

Then, the deleter is initialized with the one of other, using std::move if std::is_nothrow_move_constructible_v<D> is true. If initialization of the deleter throws an exception and std::is_nothrow_move_constructible_v<RS> is true and other owns the resource, calls the deleter of other with res_ to dispose the resource, then calls other.release().

After construction, the constructed unique_resource owns its resource if and only if other owned the resource before the construction, and other is set to not own the resource.


🌈 Parameters 🌈

  • r β€” a resource handle.
  • d β€” a deleter to use to dispose the resource.
  • other β€” another unique_resource to acquire the ownership from.

😭 Exceptions 😭

Any exception thrown during initialization of the stored resource handle or the deleter.

2) noexcept specification:

noexcept((
    std::is_nothrow_constructible_v<RS, RR> || std::is_nothrow_constructible_v<RS, RR&>
) &&
(
    std::is_nothrow_constructible_v<D, DD> || std::is_nothrow_constructible_v<D, DD&>
))

3) noexcept specification:

noexcept(
    std::is_nothrow_move_constructible_v<R1> &&
    std::is_nothrow_move_constructible_v<D>
)

πŸ’œ Notes πŸ’œ

The mechanism of these constructors ensures no leaking of resources. Literally the whole point, and it ate. πŸ”₯


πŸ“ Example πŸ“

This section is incomplete.

Reason: no example β€” not me crying over the missing example 😭.


🫢 See also 🫢

  • constructor β€” constructs a new unique_ptr (public member function of std::unique_ptr<T,Deleter>).

πŸ‘‘ Categories πŸ‘‘

  • conditionally noexcept
  • Todo no example

πŸ’– source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::experi...que_resource(3) β€” bye bestie, go construct something iconic! πŸ’…βœ¨

πŸ“¦ where this page comes from