Skip to content

Instantly share code, notes, and snippets.

@prideout
Created December 1, 2015 05:52
Show Gist options
  • Save prideout/2ef4987ed253daa7abcb to your computer and use it in GitHub Desktop.
Save prideout/2ef4987ed253daa7abcb to your computer and use it in GitHub Desktop.
automatically execute code when the scope ends
template <typename F>
struct ScopeExit {
ScopeExit(F f) : f(f) {}
~ScopeExit() { f(); }
F f;
};
template <typename F>
ScopeExit<F> MakeScopeExit(F f) {
return ScopeExit<F>(f);
};
#define SCOPE_EXIT(code) \
auto STRING_JOIN2(scope_exit_, __LINE__) = MakeScopeExit([=](){code;})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment