Created
September 21, 2010 14:09
-
-
Save gintenlabo/589728 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
#include <iostream> | |
#include <memory> | |
template<class F> | |
inline std::shared_ptr<void> scope_exit( F f ) | |
{ | |
void* const p = 0; | |
return std::shared_ptr<void>( p, [=](void*)mutable{ f(); } ); | |
} | |
void foo() | |
{ | |
std::cout << "start" << std::endl; | |
auto const _ = scope_exit( [](){ std::cout << "scope end" << std::endl; } ); | |
std::cout << "end" << std::endl; | |
} | |
int main() | |
{ | |
foo(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://d.hatena.ne.jp/faith_and_brave/20100921/1285049653 の例をちょっとだけ洗練させてみた。
std::unique_ptr を使えばキャンセルできそう。