Not an excuse to forgo RAII, but useful when e.g. using non RAII aware APIs:
template<typename Functor>
struct finally_guard {
finally_guard(Functor f)
: functor(std::move(f))
, active(true)
{}
finally_guard(finally_guard&& other)
: functor(std::move(other.functor))
, active(other.active)
{ other.active = false; }
finally_guard& operator=(finally_guard&&) = delete;
~finally_guard()
{
if(active)
functor();
}
Functor functor;
bool active;
};
template<typename F>
finally_guard<typename std::decay<F>::type>
finally(F&& f)
{
return { std::forward<F>(f) };
}
Usage:
auto resource = /* acquire */;
auto guard = finally([&resource] { /* cleanup */ });
// using just
// finally([&resource] { /* cleanup */ });
// is wrong, as usual
Note how you don't need a try
block if you don't need to translate or otherwise handle exceptions.
While my example makes use of C++11 features, the same generic functionality was available with C++03 (no lambdas though).