命名空间
变体
操作

std::experimental::scope_exit

来自 cppreference.cn
定义于头文件 <experimental/scope>
template< class EF >
class scope_exit;
(library fundamentals TS v3)

类模板 scope_exit 是一个通用的作用域守卫,旨在作用域退出时调用其退出函数。

scope_exit 不是 CopyConstructibleCopyAssignableMoveAssignable,但是,如果 EF 满足某些要求,则它可以是 MoveConstructible,这允许将 scope_exit 包装到另一个对象中。

scope_exit 可以是活动的,即在销毁时调用其退出函数;也可以是不活动的,即在销毁时什么也不做。scope_exit 在从退出函数构造后是活动的。

scope_exit 可以通过手动或自动(通过移动构造函数)调用 release() 使其变为不活动。也可以通过使用另一个不活动的 scope_exit 初始化来获得不活动的 scope_exit。一旦 scope_exit 变为不活动,它就不能再次变为活动。

scope_exit 有效地保存一个 EF 和一个指示其是否活动的 bool 标志。

内容

[编辑] 模板参数

EF - 存储的退出函数的类型
类型要求
-
EF 应当是以下之一
-
调用不带参数的 std::remove_reference_t<EF> 的左值应当是良构的。

[编辑] 成员函数

构造一个新的 scope_exit
(公共成员函数) [编辑]
如果 scope_exit 是活动的,则在作用域退出时调用退出函数,然后销毁 scope_exit
(公共成员函数) [编辑]
operator=
[已删除]
scope_exit 不可赋值
(公共成员函数)
修饰符
使 scope_exit 变为不活动
(公共成员函数) [编辑]

[编辑] 推导指引

[编辑] 备注

构造具有动态存储期的 scope_exit 可能会导致意外行为。

如果存储在 scope_exit 对象中的 EF 引用了定义它的函数的局部变量,例如,作为按引用捕获变量的 lambda,并且该变量在该函数中用作返回操作数,则当 scope_exit 的析构函数执行(调用退出函数)时,该变量可能已经被返回。这可能会导致令人惊讶的行为。

[编辑] 示例

#include <iostream>
#include <cstdlib>
#include <string_view>
#include <experimental/scope>
 
void print_exit_status(std::string_view name, bool exit_status, bool did_throw) {
  std::cout << name << ":\n";
  std::cout << "  Throwed exception  " << (did_throw ? "yes" : "no") << "\n";
  std::cout << "  Exit status        " << (exit_status ? "finished" : "pending") << "\n\n";
}
 
// Randomly throw an exception (50% chance)
void maybe_throw() {
    if (std::rand() >= RAND_MAX / 2)
        throw std::exception{};
}
 
int main() {
  bool exit_status{false}, did_throw{false};
 
  // Manual handling at "end of scope"
  try {
    maybe_throw();
    exit_status = true; 
  } catch (...) { did_throw = true; }
  print_exit_status("Manual handling", exit_status, did_throw);
 
  // Using scope_exit: runs on scope exit (success or exception)
  exit_status = did_throw = false;
  try {
    auto guard = std::experimental::scope_exit{[&]{ exit_status = true; } };
    maybe_throw();
  } catch (...) { did_throw = true; }
  print_exit_status("scope_exit", exit_status, did_throw);
 
  // Using scope_fail: runs only if an exception occurs
  exit_status = did_throw = false;
  try {
    auto guard = std::experimental::scope_fail{[&]{ exit_status = true; } };
    maybe_throw();
  } catch (...) { did_throw = true; }
  print_exit_status("scope_fail", exit_status, did_throw);
 
  // Using scope_success: runs only if no exception occurs
  exit_status = did_throw = false;
  try {
    auto guard = std::experimental::scope_success{[&]{ exit_status = true; } };
    maybe_throw();
  } catch (...) { did_throw = true; }
  print_exit_status("scope_success", exit_status, did_throw);
}

输出

Manual handling:
  Throwed exception  yes
  Exit status        pending
 
scope_exit:
  Throwed exception  no
  Exit status        finished
 
scope_fail:
  Throwed exception  yes
  Exit status        finished
 
scope_success:
  Throwed exception  yes
  Exit status        pending

[编辑] 参见

封装一个函数对象,并在通过异常退出作用域时调用它
(类模板) [编辑]
封装一个函数对象,并在正常退出作用域时调用它
(类模板) [编辑]
unique_ptr 的默认删除器
(类模板) [编辑]