名稱空間
變體
操作

std::experimental::scope_exit

來自 cppreference.com
< cpp‎ | 實驗性
定義於標頭檔案 <experimental/scope>
template< class EF >
class scope_exit;
(庫基礎 TS v3)

類模板 scope_exit 是一個通用的作用域守衛,旨在當作用域退出時呼叫其退出函式。

scope_exit 不是可複製構造的(CopyConstructible)可複製賦值的(CopyAssignable)可移動賦值的(MoveAssignable),但是,如果 EF 滿足某些要求,它可能是可移動構造的(MoveConstructible),這允許將 scope_exit 封裝到另一個物件中。

一個 scope_exit 可以是活動的(active),即在銷燬時呼叫其退出函式,也可以是不活動的(inactive),即在銷燬時什麼也不做。從退出函式構造後,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 的預設刪除器
(類模板) [編輯]