General Utility Library for C++14
2.11
|
FinalAction allows us to execute something if the FinalAction object leaves the scope.
A FinalAction can be used to add RAII like behavior for non RAII object or to do timing measurements.
To generate this object you can use the function finally, that leverages template argument deduction of the action's type. This simplifies instantiation a lot.
(Implementation is quite similar to what's in the Guideline-Support-Library.)
A good example is function call duration measurement. The FinalAction just needs to be created in the beginning of a function and no hassle with diverse possible return points.
A (bad; use containers instead) example is allocation with RAII:
F | The type of the closure/function to be called. |
#include <finalizer.h>
Public Member Functions | |
FinalAction (F f) noexcept | |
Creates a new FinalAction object. More... | |
FinalAction (FinalAction &&other) noexcept | |
Move constructor. | |
FinalAction & | operator= (FinalAction &&other) noexcept |
Move assignment operator. | |
FinalAction ()=delete | |
FinalAction is not is_default_constructible. | |
FinalAction (const FinalAction &)=delete | |
FinalAction is not copyable. | |
FinalAction & | operator= (const FinalAction &)=delete |
FinalAction is not copyable. | |
~FinalAction () noexcept | |
Destructor Calls action except when in move contexts. | |
|
inlineexplicitnoexcept |
Creates a new FinalAction object.
It takes any callable as action to be called when the FinalAction destructs (lifetime ends / leaves the scope).
The template parameter F has to be specified; this can be avoided by using the convenience function finally.
F | The type of the closure/function to be called. |
f | The closure or function to be called on destruction. |