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

active

Returns true if the scope guard is active, otherwise false.

operator=

release

Deactivates the scope guard.

scope_exit

set_active

Activates or deactivates the scope guard.

~scope_exit

If active() == true, and invoking the condition function object returns true, invokes the wrapped callable action function object. Destroys the function objects.

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 to true, if the action function object is allowed to be executed, and false otherwise. Additionally, the condition function object operator() 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 returns true. 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 returns true, 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 calling set_active(false) or release(). An inactive scope guard can be made active by calling set_active(true). If a moved-from scope guard is active on destruction, the behavior is undefined.