## Class template scope_exit

boost::scope::scope_exit — Scope exit guard that conditionally invokes a function upon leaving the scope.

## Synopsis

    // In header: <boost/scope/scope_exit.hpp>

    template<typename Func, typename Cond = always_true>
    class scope_exit {
    public:

      // public member functions
      template<typename F> explicit scope_exit(F &&, bool = true);
      template<typename F, typename C>
        explicit scope_exit(F &&, C &&, bool = true);
      scope_exit(scope_exit &&);
      scope_exit & operator=(scope_exit &&) = delete;
      scope_exit(scope_exit const &) = delete;
      scope_exit & operator=(scope_exit const &) = delete;
      ~scope_exit();
      bool active() const noexcept;
      void set_active(bool) noexcept;
    };

## 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)`. 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.

### Template Parameters

1.     typename Func

   Scope guard action function object type.

2.     typename Cond = always_true

   Scope guard condition function object type.

### `scope_exit` public member functions

1.     template<typename F> explicit scope_exit(F && func, bool active = true);

   Constructs a scope guard with a given callable action function object.

   Requires: `Func` is constructible from func. `Cond` is nothrow default-constructible and is not a pointer to function.

   | Note
   The requirement for `Cond` default constructor to be non-throwing is to allow for the condition function object to be called in case if constructing either function object throws.

   Effects: Constructs the scope guard as if by calling `scope_exit(std::forward< F >(func), Cond(), active)`.

   Throws: Nothing, unless construction of the function objects throw.

   Parameters: |
   `func` | The callable action function object to invoke on destruction.
   `active` | Indicates whether the scope guard should be active upon construction.
   Postconditions: | `this->active() == active`

2.     template<typename F, typename C>
         explicit scope_exit(F && func, C && cond, bool active = true);

   Constructs a scope guard with a given callable action and condition function objects.

   Requires: `Func` is constructible from func. `Cond` is constructible from cond.

   Effects: If `Func` is nothrow constructible from `F&&` then constructs `Func` from `std::forward< F >(func)`, otherwise constructs from `func`. If `Cond` is nothrow constructible from `C&&` then constructs `Cond` from `std::forward< C >(cond)`, otherwise constructs from `cond`.

   If `Func` or `Cond` construction throws and active is `true`, invokes cond and, if it returns `true`, func before returning with the exception.

   Throws: Nothing, unless construction of the function objects throw.

   Parameters: |
   `func` | The callable action function object to invoke on destruction.
   `cond` | The callable condition function object.
   `active` | Indicates whether the scope guard should be active upon construction.
   Postconditions: | `this->active() == active`

3.     scope_exit(scope_exit && that);

   Move-constructs a scope guard.

   Requires: `Func` and `Cond` are nothrow move-constructible or copy-constructible.

   Effects: If `Func` is nothrow move-constructible then move-constructs `Func` from a member of that, otherwise copy-constructs `Func`. If `Cond` is nothrow move-constructible then move-constructs `Cond` from a member of that, otherwise copy-constructs `Cond`.

   If `Func` or `Cond` construction throws and `that.active() == true`, invokes `Cond` object stored in that and, if it returns `true`, Func object (either the newly constructed one, if its construction succeeded, or the original one stored in that) before returning with the exception.

   If the construction succeeds, marks that as inactive.

   Throws: Nothing, unless move-construction of the function objects throw.

   Parameters: |
   `that` | Move source.
   Postconditions: | `that.active() == false`

4.     scope_exit & operator=(scope_exit &&) = delete;

5.     scope_exit(scope_exit const &) = delete;

6.     scope_exit & operator=(scope_exit const &) = delete;

7.     ~scope_exit();

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

   Throws: Nothing, unless invoking a function object throws.

8.     bool active() const noexcept;

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

   | Note
   This method does not call the condition function object specified on construction.

   Throws: Nothing.

9.     void set_active(bool active) noexcept;

   Activates or deactivates the scope guard.

   Throws: Nothing.

   Parameters: |
   `active` | The active status to set.
   Postconditions: | `this->active() == active`