#pragma once template class ScopeExit { T mFunc; public: ScopeExit(T aFunc) : mFunc(aFunc) { } ~ScopeExit() { mFunc(); } }; template ScopeExit onScopeExit(F f) { return ScopeExit(f); }; #define __strcat2(s1, s2) s1 ## s2 #define __strcat1(s1, s2) __strcat2(s1, s2) #define ON_SCOPE_EXIT(...) \ auto __strcat1(onExit, __LINE__) = onScopeExit( [&](void) {__VA_ARGS__} )