std::scope_exit is a generic RAII wrapper over any callable (callable class, lambda, function). It calls it's function at the scope end. You can find more information in http://wg21.link/p0052
A typical usage is freeing resources. Imagine some function where you do malloc() at the beginning and later on free() at three different places before each return;. With scope guard you can just do something like
auto _ = std::make_scope_guard([ptr]() { free(ptr); });
|
once soon after allocating and forget about possibly memory leaks.