std::experimental::scope_fail
來自 cppreference.com
定義於標頭檔案 <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) | |
(C++11) |
unique_ptr 的預設刪除器 (class template) |