View on GitHub


A modern C++ scope guard that is easy to use but hard to misuse.


Build Status Build status codecov

GitHub release Vcpkg semver GitHub license

GitHub stars

A public, general, simple, and fast C++11 scope guard that defends against implicitly ignored returns and optionally enforces noexcept at compile time (in C++17), all in a SFINAE-friendly maner.


Get it here. Usage is simple:

#include "scope_guard.hpp"
  auto guard = sg::make_scope_guard(my_callback);
} // my_callback is invoked at this point


A scope guard is an object that employs RAII to execute a provided callback when leaving scope, be it through a fall-through, a return, or an exception. That callback can be a function, a function pointer, a functor, a lambda, a bind result, a std::function, a reference to any of these, or any other callable, as long as it respects a few preconditions – most of which are enforced during compilation, the rest being hopefully intuitive.

All necessary code is provided in a single header (the remaining files are only for testing and documentation).


The concept of “scope guard” was proposed by Andrei Alexandrescu and Petru Marginean and it is well known in the C++ community. It has been proposed for standardization (see N4189) but is still not part of the standard library, as of March 2018.


While there are several implementations available, I did not find any with all the characteristics I aimed for here - safe, tested, documented, public domain, thin wrapping, general, standalone, simple interface… (see feature list below).


Main features

Other characteristics


The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119.


Bug reports and suggestions are welcome. If you find that something is incorrect or could be improved, feel free to open an issue.


Setup consists merely of making the header file available to the compiler. That can be achieved by any of the following options:

The preprocessor definition SG_REQUIRE_NOEXCEPT_IN_CPP17 MAY be provided to the compiler. The effect of this option is explained here.

Further documentation

Client interface

The client interface is documented in detail here.

Preconditions in detail

Callback preconditions are explained here.

Design choices and concepts

Design choices and concepts are discussed here.


Instructions on how to run the tests are here.