Skip to content

Instantly share code, notes, and snippets.

@socantre
Created July 20, 2017 17:47
Show Gist options
  • Save socantre/7276a756f704569afed67b839ba54861 to your computer and use it in GitHub Desktop.
Save socantre/7276a756f704569afed67b839ba54861 to your computer and use it in GitHub Desktop.
simple scope exit
template <typename Func> struct scope_exit
{
Func cleanup;
scope_exit(Func &&f) : cleanup(std::forward<Func>(f)) {}
~scope_exit() { cleanup(); }
};
template <typename Func> scope_exit<Func> make_scope_exit(Func &&f)
{
return scope_exit<Func>(std::forward<Func>(f));
}
#define SCOPE_EXIT_IMPL2(l, cl) auto SCOPE_EXIT_##l##_exit = make_scope_exit(cl)
#define SCOPE_EXIT_IMPL1(l, cl) SCOPE_EXIT_IMPL2(l, cl)
#define SCOPE_EXIT(...) SCOPE_EXIT_IMPL1(__LINE__, ([&] { __VA_ARGS__ }))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment