Skip to content

Instantly share code, notes, and snippets.

@tsaarni
Created August 21, 2015 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaarni/f27afa654cc10d99a40b to your computer and use it in GitHub Desktop.
Save tsaarni/f27afa654cc10d99a40b to your computer and use it in GitHub Desktop.
// 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