Scope Guard#

#include <dplx/scope_guard.hpp>
namespace dplx {}
template<typename EF>
struct scope_exit#

See also

This class matches the scope_exit specified by the Library Fundamentals TS v3.

Utility class for executing a function at scope end, the execution order is determined by destruction order e.g.:

{
    scope_exit cleanup1 = [&] { printf("cleanup1\n"); };
    scope_exit cleanup2 = [&] { printf("cleanup2\n"); };

    if (condition)
    {
        return; // prints "cleanup2\ncleanup1\n" on early return
    }
    /*...*/

} // prints "cleanup2\ncleanup1\n" while destructing cleanup on scope end

In contrast to scope_guard<Fn> it is possible to move “ownership” of the finalization or outright cancel it.

Warning

The given functor will be executed during object destruction. If an exception escapes, std::terminate() will be invoked.

scope_exit(scope_exit &&other) noexcept(__see_below__)#

Imports the finalization task from other.

This constructor is noexcept if is_nothrow_move_constructible_v<EF> || is_nothrow_copy_constructible_v<EF> holds.

Parameters:

scope_exit &&other – The instance to be moved into this.

template<typename Fn>
scope_exit(Fn &&fn) noexcept(__see_below__)#

This constructor is noexcept if is_nothrow_constructible_v<EF, Fn> || is_nothrow_constructible_v<EF, Fn&> holds.

Parameters:

Fn &&fn – The function to be executed at destruction; usually a lambda.

void release() noexcept#

Cancels execution of the attached finalization logic.

template<typename Fn>
struct scope_guard#

Utility class for executing a function at scope end, the execution order is determined by destruction order e.g.:

{
    scope_guard cleanup1 = [&] { printf("cleanup1\n"); };
    scope_guard cleanup2 = [&] { printf("cleanup2\n"); };

    if (condition)
    {
        return; // prints "cleanup2\ncleanup1\n" on early return
    }
    /*...*/

} // prints "cleanup2\ncleanup1\n" while destructing cleanup on scope end

Warning

The given functor will be executed during object destruction. If an exception escapes, std::terminate() will be invoked.

scope_guard(Fn &&fn)#
Parameters:

Fn &&fn – The function to be executed at destruction; usually a lambda.

template<typename Fn>
struct [[deprecated]] exception_scope_guard#

Utility class for executing a function during stack unwinding, e.g.:

{
    exception_scope_guard cleanup = [&]() { printf("cleanup\n"); };

    if (condition)
    {
        throw std::runtime_error(); // prints "cleanup" during stack unwinding
    }
    /*...*/

} // nothing is printed

Warning

The given functor will be executed during object destruction. If an exception escapes, std::terminate() will be invoked.

exception_scope_guard(Fn &&fn)#
Parameters:

Fn &&fn – The function to be executed during stack unwinding