命名空间
变体
操作

std::experimental::scope_fail

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

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

scope_fail 不是 可复制构造, 可复制赋值可移动赋值,但是,如果 EF 满足某些要求,则它可以是 可移动构造,这允许将 scope_fail 包装到另一个对象中。

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

通过手动或自动(通过移动构造函数)调用 release() 可以使 scope_fail 变为非激活状态。也可以通过使用另一个非激活的 scope_fail 初始化来获得非激活的 scope_fail。一旦 scope_fail 变为非激活状态,它就无法再次变为激活状态。

scope_fail 有效地保存一个 EF 和一个指示其是否激活的 bool 标志,以及一个用于检测析构函数是否在栈展开期间被调用的未捕获异常计数器。

目录

[编辑] 模板参数

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

[编辑] 成员函数

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

[编辑] 推导指引

[编辑] 注意

构造具有动态存储持续时间的 scope_fail 可能会导致意外行为。

从在不同线程中创建的另一个 scope_fail 构造 scope_fail 也可能导致意外行为,因为在销毁期间可能会比较在不同线程中获得的未捕获异常计数。

[编辑] 示例

#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 的默认删除器
(类模板) [编辑]