Namespaces
Variants

std::experimental:: scope_exit

From cppreference.net

ヘッダーで定義 <experimental/scope>
template < class EF >
class scope_exit ;
(ライブラリ基盤 TS v3)

クラステンプレート scope_exit は、スコープが終了した際に終了関数を呼び出すことを目的とした汎用スコープガードです。

scope_exit CopyConstructible CopyAssignable または MoveAssignable ではありません。ただし、 EF が特定の要件を満たす場合、 MoveConstructible である可能性があり、これにより scope_exit を別のオブジェクトでラップすることが可能になります。

A scope_exit は、破棄時に終了関数を呼び出すアクティブ状態、または破棄時に何も行わない非アクティブ状態のいずれかになります。 scope_exit は、終了関数から構築された後はアクティブ状態です。

scope_exit は、手動または自動的に(ムーブコンストラクタによって)その release() を呼び出すことで非アクティブになります。非アクティブな scope_exit は、別の非アクティブな scope_exit で初期化することによっても取得できます。 scope_exit が一度非アクティブになると、再度アクティブにすることはできません。

A scope_exit は実質的に EF と、アクティブかどうかを示す bool フラグを保持します。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Template parameters、Member functions、Modifiers、Deduction guides、Notes、Example、See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、ID、リンク先は一切変更していません - 数値や構造は完全に保持されています

テンプレートパラメータ

EF - 格納される終了関数の型
型要件
-
EF は以下のいずれかでなければならない:
-
引数なしで std:: remove_reference_t < EF > の左辺値を呼び出すことは well-formed でなければならない。

メンバー関数

新しい scope_exit を構築する
(公開メンバ関数)
スコープが終了する際に scope_exit がアクティブな場合、終了関数を呼び出し、 scope_exit を破棄する
(公開メンバ関数)
operator=
[deleted]
scope_exit は代入不可
(公開メンバ関数)
修飾子
scope_exit を非アクティブにする
(公開メンバ関数)

推論ガイド

注記

動的ストレージ期間を持つ scope_exit の構築は、予期せぬ動作を引き起こす可能性があります。

scope_exit オブジェクトに格納された EF が、例えば参照によって変数をキャプチャするラムダのように、それが定義された関数のローカル変数を参照している場合、かつその変数がその関数のreturnオペランドとして使用されている場合、 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";
}
// ランダムに例外をスロー(50%の確率)
void maybe_throw() {
    if (std::rand() >= RAND_MAX / 2)
        throw std::exception{};
}
int main() {
  bool exit_status{false}, did_throw{false};
  // 「スコープ終了時」での手動処理
  try {
    maybe_throw();
    exit_status = true; 
  } catch (...) { did_throw = true; }
  print_exit_status("Manual handling", exit_status, did_throw);
  // scope_exitの使用:スコープ終了時(成功時または例外発生時)に実行
  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);
  // scope_failの使用:例外が発生した場合のみ実行
  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);
  // scope_successの使用:例外が発生しなかった場合のみ実行
  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 のデフォルト削除子
(クラステンプレート)