What the OP describes here is a common C++ pattern known as "scope guard" (check "Solution 4" in <a href="http://drdobbs.com/184403758" rel="nofollow">http://drdobbs.com/184403758</a>). It was possible to implement it in C++03 with some tricks but C++11 lambdas make it much easier.<p>This is a very nice small implementation but, as always with C++, devil is in the details:<p>* Creation of an std::function needs a dynamic allocation, so if the allocation fails an exception will be thrown and if the <i>finally</i> is guarding a resource, the resource will be leaked<p>* std::function has a non-negligible calling overhead, hence this should not be used in performance-sensitive code<p>* Checking a condition inside the finally clause is not very elegant, a better idiom in C++ is to support <i>dismissing</i> a scope guard.<p>* The finallyClause may throw an exception, and since it is called in a destructor this is generally considered a bad idea. I don't know what could happen in this case, but some scope guard implementations I've seen catch the exception and explicitly call std::terminate(). I guess this is for performance reasons, because the destructor can be declared nothrow.<p>Here there is a more complex implementation, which addresses most of the corner cases:<p><a href="http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-6-of-n#c634477472460000000" rel="nofollow">http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Stepha...</a>