命名空间
变体
操作

std::experimental::scope_success

来自 cppreference.cn
定义于头文件 <experimental/scope>
template< class EF >
class scope_success;
(库基础 TS v3)

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

scope_success 不是 可复制构造 (CopyConstructible), 可复制赋值 (CopyAssignable)可移动赋值 (MoveAssignable),但是,如果 EF 满足某些要求,则它可能是 可移动构造 (MoveConstructible) 的,这允许将 scope_success 包装到另一个对象中。

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

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

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

目录

[编辑] 模板形参

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

[编辑] 成员函数

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

[编辑] 推导指引

[编辑] 注解

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

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

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

[编辑] 示例

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