template <typename T>
muu::scope_fail class

Performs actions when going out of scope due to an exception being thrown.

Template parameters
T A noexcept function, lambda or other callable not requiring any arguments (e.g. void() noexcept)`.

Use a scope_fail to simplify cleanup routines or code that has acquire/release semantics:

void* get_initialized_buffer()
{
    void* buffer = acquire(1024);
    muu::scope_fail err{ [=]() noexcept { release(buffer); }};
    //
    // ...a bunch of initialization code that might throw...
    //
    return buffer;
}

For comparison's sake, here's the same function without a scope_fail:

void* get_initialized_buffer()
{
    void* buffer = acquire(1024);
    try
    {
        //
        // ...a bunch of initialization code that might throw...
        //
    }
    catch (...)
    {
        release(buffer);
        throw;
    }
    return buffer;
}

Constructors, destructors, conversion operators

template <typename U>
scope_fail(U&& func) explicit noexcept(…)
Constructs a scope_fail by wrapping a callable.

Public functions

void dismiss() noexcept
Dismisses the scope guard, cancelling invocation of the wrapped callable.
auto dismissed() const -> bool noexcept
Returns true if the scope guard has been dismissed.

Function documentation

template <typename T>
template <typename U>
muu::scope_fail::scope_fail(U&& func) explicit noexcept(…)

Constructs a scope_fail by wrapping a callable.

Template parameters
U A function, lambda or other callable with the signature void() noexcept.
Parameters
func The callable to invoke when the scope guard goes out of scope.