CppDS.com

C++ 98 11 14 17 20 手册

std::experimental::unique_resource<R, D>::unique_resource

来自cppreference.com
unique_resource();
(1) (库基础 TS v3)
template <class RR, class DD>
unique_resource( RR&& r, DD&& d ) noexcept(/*see below*/)
(2) (库基础 TS v3)
unique_resource( unique_resource&& other );
(3) (库基础 TS v3)

为解释目的使用下列项目:

  • RS 为存储的资源柄的类型。
  • 表达式 res_ 指代底层资源柄。
  • del_ 指代删除器对象。
1) 默认构造函数。值初始化存储的资源柄与删除器。构造的的 unique_resource 不拥有资源。
此重载仅若 std::is_default_constructible_v<R>std::is_default_constructible_v<D> 均为 true 才参与重载决议。
2)std::is_nothrow_constructible_v<RS, RR>true 则以 std::forward<RR>(r) ,否则以 r 初始化存储的资源柄。若存储的资源柄的初始化抛异常,则调用 d(r)
然后,若 std::is_nothrow_constructible_v<D, DD>true 则以 std::forward<DD>(d) ,否则以 d 初始化删除器。若删除器的初始化跑一次,则调用 d(res_)
构造的 unique_resource 占有资源。
此重载仅若 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&> 都为 true 才参与重载决议。
若表达式 d(r)d(res_)del_(res_) 的任一者为谬构,则程序为谬构。
若表达式 d(r)d(res_)del_(res_) 的任一者导致未定义行为或抛异常,则行为未定义。
3) 移动构造函数。用 other 的存储的资源柄初始化存储的资源柄,若 std::is_nothrow_move_constructible_v<RS>true 则使用 std::move 。若底层资源柄的初始化抛异常,则不修改 other
然后,用 other 的删除器初始化删除器,若 std::is_nothrow_move_constructible_v<D>true 则使用 std::move 。若删除器的初始化抛异常且 std::is_nothrow_move_constructible_v<RS>trueother 占有资源,则以 res_ 调用 other 的删除器释放资源,然后调用 other.release()
构造后,构造的 unique_resource 占有资源当且仅当 other 在构造前占有资源,并设置 other 为不占有资源。

参数

r - 资源柄
d - 用于释放资源的删除器
other - 要获得其所有权的 unique_resource

异常

任何初始化存储的资源柄或删除器时抛出的异常。

2)
noexcept 规定:  

注解

这些构造函数的机制确保无资源泄漏。

示例

参阅

构造新的unique_ptr
(std::unique_ptr<T,Deleter> 的公开成员函数)
关闭