Scope exit guard that conditionally invokes a function upon leaving the scope.
Synopsis
Declared in header <boost/scope/scope_exit.hpp>
template<
typename Func,
typename Cond = always_true>
class scope_exit;
Member Functions
Name |
Description |
Returns |
|
Deactivates the scope guard. |
|
Activates or deactivates the scope guard. |
|
If |
Description
The scope guard wraps two function objects: the scope guard action and a condition for invoking the action. Both function objects must be callable with no arguments and can be one of:
-
A user-defined class with a public
operator()
. -
An lvalue reference to such class.
-
An lvalue reference or pointer to function taking no arguments. The condition function object
operator()
must return a value contextually convertible totrue,
if the action function object is allowed to be executed, andfalse
otherwise. Additionally, the condition function objectoperator()
must not throw, as otherwise the action function object may not be called. The condition function object is optional, and if not specified in template parameters, the scope guard will operate as if the condition always returnstrue.
The scope guard can be in either active or inactive state. By default, the constructed scope guard is active. When active, and condition function object returnstrue,
the scope guard invokes the wrapped action function object on destruction. Otherwise, the scope guard does not call the wrapped action function object. The scope guard can be made inactive by moving-from the scope guard or callingset_active(false)
orrelease()
. An inactive scope guard can be made active by callingset_active(true)
. If a moved-from scope guard is active on destruction, the behavior is undefined.