Namespaces
Variants

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

From cppreference.net
unique_resource ( ) ;
(1) (库基础技术规范 v3)
template < class RR, class DD >
unique_resource ( RR && r, DD && d ) noexcept ( /*见下文*/ )
(2) (库基础技术规范 v3)
unique_resource ( unique_resource && other ) ;
(3) (库基础技术规范 v3)

以下项目用于说明目的:

  • RS 是存储资源句柄的类型。
  • 表达式 res_ 指向底层资源句柄。
  • del_ 指向删除器对象。
1) 默认构造函数。值初始化存储的资源句柄和删除器。构造的 unique_resource 不拥有该资源。
此重载仅当 std:: is_default_constructible_v < R > std:: is_default_constructible_v < D > 均为 true 时参与重载决议。
2) 存储的资源句柄将使用 std:: forward < RR > ( r ) 进行初始化(当 std:: is_nothrow_constructible_v < RS, RR > true 时),否则使用 r 。若存储资源句柄的初始化抛出异常,则调用 d ( r )
随后,删除器将使用 std:: forward < DD > ( d ) 进行初始化(当 std:: is_nothrow_constructible_v < D, DD > true 时),否则使用 d 。若删除器的初始化抛出异常,则调用 d ( res_ )
构造完成的 unique_resource 拥有该资源的所有权。
此重载仅当以下所有条件均为 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 & >
如果表达式 d ( r ) d ( res_ ) del_ ( res_ ) 中有任何一个格式不正确,则程序是非良构的。
若表达式 d ( r ) d ( res_ ) del_ ( res_ ) 中的任一表达式导致未定义行为或抛出异常,则行为是未定义的。
3) 移动构造函数。存储的资源句柄使用 std::move other 的句柄初始化(当 std:: is_nothrow_move_constructible_v < RS > true 时)。若存储资源句柄的初始化抛出异常,则 other 保持不变。
随后,删除器使用 std::move other 的删除器初始化(当 std:: is_nothrow_move_constructible_v < D > true 时)。若删除器初始化抛出异常,且 std:: is_nothrow_move_constructible_v < RS > true other 拥有资源,则调用 other 的删除器并传入 res_ 以释放资源,随后调用 other. release ( )
构造完成后,若 other 在构造前拥有资源,则构造的 unique_resource 拥有该资源,同时 other 被设置为不拥有资源。

目录

参数

r - 资源句柄
d - 用于释放资源的删除器
other - 要从中获取所有权的另一个 unique_resource

异常

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

注释

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

示例

参见

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