Repository navigation
Scope Guard & Defer C++ v0.10.0
scope_guard is a header-only C++11 library for running cleanup and rollback code when a scope exits. It provides SCOPE_EXIT, SCOPE_FAIL, SCOPE_SUCCESS, DEFER and the make_scope_* factories.
This release focuses on correctness, exception safety and compiler compatibility.
Highlights
WITH_SCOPE_*creates the guard before the block. Actions now run onbreak,continue,returnand exceptions, andWITH_SCOPE_FAILworks. Actions may contain commas.- If moving the action into a new guard throws,
scope_exitandscope_failstill run the action.scope_successdoes not. - Actions with throwing destructors are handled correctly.
SCOPE_GUARD_NO_THROW_ACTIONandSCOPE_GUARD_SUPPRESS_THROW_ACTIONrequire non-throwing action destructors. SCOPE_GUARD_SUPPRESS_THROW_ACTIONbuilds with Clang-fexceptions -fno-cxx-exceptions.- C++11/14 builds with Clang no longer conflict with
<cxxabi.h>. - Clear compile errors for unsupported actions, such as lvalues, const rvalues and non-movable callables. Use
std::moveorstd::reffor named callables. - Clean strict-warning builds on GCC, Clang 22/23 and MSVC. MSVC no longer hides warnings inside actions.
- Guards cannot be allocated with
newornew[].
Compatibility
- Requires C++11 or newer, with GCC 9+, Clang 9+ or Visual Studio 2019+ (v142). CMake 3.15+ is required.
- On Clang 22 with
-pedantic, unnamed guards inside another macro's arguments warn about__COUNTER__. Use a named guard, a factory or Clang 23.