Technical Specification | ||||
Filesystem library(filesystem TS) | ||||
Library fundamentals(library fundamentals TS) | ||||
Library fundamentals 2(library fundamentals TS v2) | ||||
Library fundamentals 3(library fundamentals TS v3) | ||||
Extensions for parallelism(parallelism TS) | ||||
Extensions for parallelism 2(parallelism TS v2) | ||||
Extensions for concurrency(concurrency TS) | ||||
Extensions for concurrency 2(concurrency TS v2) | ||||
Concepts(concepts TS) | ||||
Ranges(ranges TS) | ||||
Reflection(reflection TS) | ||||
Mathematical special functions(special functions TR) | ||||
Experimental Non-TS | ||||
Pattern Matching | ||||
Linear Algebra | ||||
std::execution | ||||
Contracts | ||||
2D Graphics |
experimental::scope_success | ||||
Defined in header <experimental/scope> | ||
template<class EF> class scope_success; | (library fundamentals TS v3) | |
The class templatescope_success
is a general-purpose scope guard intended to call its exit function when a scope is normally exited.
scope_success
is notCopyConstructible,CopyAssignable orMoveAssignable, however, it may beMoveConstructible ifEF
meets some requirements, which permits wrapping ascope_success
into another object.
Ascope_success
may be either active, i.e. calls its exit function on destruction, or inactive, i.e. does nothing on destruction. Ascope_success
is active after constructed from an exit function.
Ascope_success
can become inactive by callingrelease() on it either manually or automatically (by the move constructor). An inactivescope_success
may also be obtained by initializing with another inactivescope_success
. Once ascope_success
is inactive, it cannot become active again.
Ascope_success
effectively holds anEF
and abool flag indicating if it is active, alongwith a counter of uncaught exceptions used for detecting whether the destructor is called during stack unwinding.
Contents |
EF | - | type of stored exit function |
Type requirements | ||
-EF shall be either:
| ||
-Calling an lvalue ofstd::remove_reference_t<EF> with no argument shall be well-formed. |
constructs a newscope_success (public member function)[edit] | |
calls the exit function when the scope is exited normally if thescope_success is active, then destroys thescope_success (public member function)[edit] | |
operator= [deleted] | scope_success is not assignable(public member function) |
Modifiers | |
makes thescope_success inactive(public member function)[edit] |
Constructing ascope_success
of dynamic storage duration might lead to unexpected behavior.
Constructing ascope_success
is constructed from anotherscope_success
created in a different thread might also lead to unexpected behavior since the count of uncaught exceptions obtained in different threads may be compared during the destruction.
If theEF
stored in ascope_success
object refers to a local variable of the function where it is defined, e.g., as a lambda capturing the variable by reference, and that variable is used as a return operand in that function, that variable might have already been returned when thescope_success
's destructor executes, calling the exit function. This can lead to surprising behavior.
#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)throwstd::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);}
Output:
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
wraps a function object and invokes it on exiting the scope (class template)[edit] | |
wraps a function object and invokes it on exiting the scope through an exception (class template)[edit] | |
(C++11) | default deleter forunique_ptr (class template)[edit] |