Created
August 21, 2015 15:47
-
-
Save tsaarni/f27afa654cc10d99a40b to your computer and use it in GitHub Desktop.
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
// clang++ -Wall -std=c++11 -O0 -o scope-exit scope-exit.cpp && ./scope-exit | |
// g++ -Wall -std=c++11 -O0 -o scope-exit scope-exit.cpp && ./scope-exit | |
#include <iostream> | |
template <typename F> | |
struct ScopeExit | |
{ | |
ScopeExit(F f) : f(f) {} | |
~ScopeExit() { f(); } | |
F f; | |
}; | |
template <typename F> | |
ScopeExit<F> make_scope_exit(F f) | |
{ | |
return ScopeExit<F>(f); | |
}; | |
int | |
main(int argc, char *argv[]) | |
{ | |
auto my_scope_guard = make_scope_exit([]() { std::cout << "hello scope!" << std::endl; }); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment