std::experimental::scope_success
定義於標頭檔案 <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
從退出函式構造後是活動的。
一個 scope_success
可以透過手動或自動(透過移動建構函式)呼叫其 release() 而變為非活動狀態。透過用另一個非活動的 scope_success
初始化,也可以獲得一個非活動的 scope_success
。一旦一個 scope_success
變為非活動狀態,它就不能再次變為活動狀態。
一個 scope_success
實際上持有一個 EF
和一個 bool 標誌,指示它是否處於活動狀態,以及一個用於檢測解構函式是否在堆疊展開期間被呼叫的未捕獲異常計數器。
目錄 |
[編輯] 模板引數
EF | - | 儲存的退出函式的型別 |
型別要求 | ||
-EF 應為以下之一:
| ||
-使用無引數呼叫 std::remove_reference_t<EF> 的左值應是合法的。 |
[編輯] 成員函式
構造一個新的 scope_success (public member function) | |
如果 scope_success 處於活動狀態,則在作用域正常退出時呼叫退出函式,然後銷燬 scope_success (public member function) | |
operator= [已刪除] |
scope_success 不可賦值(公開成員函式) |
修改器 | |
使 scope_success 變為非活動狀態(public member function) |
[編輯] 推導指南
[編輯] 注意
構造具有動態儲存持續時間的 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
[編輯] 另見
封裝一個函式物件並在退出作用域時呼叫它 (類模板) | |
封裝一個函式物件並在透過異常退出作用域時呼叫它 (類模板) | |
(C++11) |
unique_ptr 的預設刪除器 (類模板) |