命名空间
变体
操作

std::experimental::scope_fail

来自 cppreference.cn
< cpp‎ | 实验性
定义于头文件 <experimental/scope>
template< class EF >
class scope_fail;
(库基础 TS v3)

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

scope_fail 不可 CopyConstructible、不可 CopyAssignable 且不可 MoveAssignable,但是,如果 EF 满足某些要求,它可能是 MoveConstructible 的,这允许将 scope_fail 包装到另一个对象中。

scope_fail 可以是活动的(即在析构时调用其退出函数),也可以是非活动的(即在析构时不做任何事情)。从退出函数构造后,scope_fail 是活动的。

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

scope_fail 实际上持有一个 EF 和一个 bool 标志,指示它是否处于活动状态,以及一个未捕获异常的计数器,用于检测析构函数是否在栈展开期间被调用。

目录

[编辑] 模板参数

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

[编辑] 成员函数

构造一个新的 scope_fail
(public member function) [编辑]
如果 scope_fail 是活动的,则当作用域因异常而退出时调用退出函数,然后销毁 scope_fail
(public member function) [编辑]
operator=
[已删除]
scope_fail 不可赋值
(公开成员函数)
修改器
使 scope_fail 变为非活动状态
(public member function) [编辑]

[编辑] 推导指南

[编辑] 注意

构造动态存储期的 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

[编辑] 另见

封装一个函数对象并在退出作用域时调用它
(class template) [编辑]
封装一个函数对象并在正常退出作用域时调用它
(class template) [编辑]
unique_ptr 的默认删除器
(class template) [编辑]