std::experimental::scope_success

From cppreference.com

 
 
Experimental
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
 
 
std::experimental::scope_success
 
Defined in header <experimental/scope>
template< class EF >
class scope_success;
(library fundamentals TS v3)

The class template scope_success is a general-purpose scope guard intended to call its exit function when a scope is normally exited.

scope_success is not CopyConstructible, CopyAssignable or MoveAssignable, however, it may be MoveConstructible if EF meets some requirements, which permits wrapping a scope_success into another object.

A scope_success may be either active, i.e. calls its exit function on destruction, or inactive, i.e. does nothing on destruction. A scope_success is active after constructed from an exit function.

A scope_success can become inactive by calling release() on it either manually or automatically (by the move constructor). An inactive scope_success may also be obtained by initializing with another inactive scope_success. Once a scope_success is inactive, it cannot become active again.

A scope_success effectively holds an EF and a bool flag indicating if it is active, alongwith a counter of uncaught exceptions used for detecting whether the destructor is called during stack unwinding.

Template parameters

EF - type of stored exit function
Type requirements
-
EF shall be either:
-
Calling an lvalue of std::remove_reference_t<EF> with no argument shall be well-formed.

Member functions

constructs a new scope_success
(public member function)
calls the exit function when the scope is exited normally if the scope_success is active, then destroys the scope_success
(public member function)
operator=
[deleted]
scope_success is not assignable
(public member function)
Modifiers
makes the scope_success inactive
(public member function)

Deduction guides

Notes

Constructing a scope_success of dynamic storage duration might lead to unexpected behavior.

Constructing a scope_success is constructed from another scope_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 the EF stored in a scope_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 the scope_success's destructor executes, calling the exit function. This can lead to surprising behavior.

Example

See also

wraps a function object and invokes it on exiting the scope
(class template)
wraps a function object and invokes it on exiting the scope through an exception
(class template)
default deleter for unique_ptr
(class template)