#include <muu/scope_guard.h>
template <typename T>
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_
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_
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
Function documentation
template <typename T>
template <typename U>
muu::scope_fail::scope_fail(U&& func) explicit noexcept(…)
template <typename U>
Constructs a scope_
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. |