CppDS.com

C++ 98 11 14 17 20 手册

std::experimental::scope_exit<EF>::scope_exit

来自cppreference.com

template <class Fn>
explicit scope_exit( Fn&& fn ) noexcept(/*see below*/);
(1) (库基础 TS v3)
scope_exit( scope_exit&& other ) noexcept(/*see below*/);
(2) (库基础 TS v3)
scope_exit( const scope_exit& ) = delete;
(3) (库基础 TS v3)

从函数、函数对象或另一 scope_exit 创建 scope_exit

1) 以函数或函数对象初始化退出函数。构造的 scope_exit 为活跃。
Fn 不是左值引用类型且 std::is_nothrow_constructible_v<EF, Fn>true ,则以 std::forward<Fn>(fn) ,否则以 fn 初始化存储的 EF
若初始化存储的 EF 抛异常,则调用 fn()
此重载仅若 std::is_same_v<std::remove_cvref_t<Fn>, scope_exit>falsestd::is_constructible_v<EF, Fn>true 才参与重载决议。
若函数调用表达式 fn() 为谬构,则程序为谬构。
若调用 fn() 抛出异常或导致未定义行为,则行为未定义,即使未调用 fn
2) 移动构造函数。以 other 的存储的 EF 初始化存储的 EF 。构造的 scope_exit 为活跃当且仅当 other 在构造前活跃。
std::is_nothrow_move_constructible_v<EF>true ,则以 std::forward<EF>(other.exitfun) ,否则以 other.exitfun 初始化存储的 EF (以 exitfun 代表)。
在成功移动构造后调用 other.release()other 变为不活跃。
此重载仅若 std::is_nothrow_move_constructible_v<EF>truestd::is_copy_constructible_v<EF>true 才参与重载决议。
若有下列情况则行为未定义

参数

fn - 用于初始化存储的 EF 的函数或函数对象
other - 要移动的 scope_exit

异常

初始化存储的 EF 期间抛出的任何异常。

1)
noexcept 规定:  
noexcept(std::is_nothrow_constructible_v<EF, Fn> ||
         std::is_nothrow_constructible_v<EF, Fn&>)
2)

示例

参阅

使 scope_exit 不活跃
(公开成员函数)
关闭