std::experi...que_resource(3)
πΈ 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:
RSis 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 expressionsd(r),d(res_)anddel_(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β anotherunique_resourceto 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 newunique_ptr(public member function ofstd::unique_ptr<T,Deleter>).
π Categories π
conditionally noexceptTodo 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
- 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