Created
July 20, 2017 17:47
-
-
Save socantre/7276a756f704569afed67b839ba54861 to your computer and use it in GitHub Desktop.
simple scope exit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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