Skip to content

Instantly share code, notes, and snippets.

@gintenlabo
Created September 21, 2010 14:09
Show Gist options
  • Save gintenlabo/589728 to your computer and use it in GitHub Desktop.
Save gintenlabo/589728 to your computer and use it in GitHub Desktop.
#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();
}
@gintenlabo
Copy link
Author

http://d.hatena.ne.jp/faith_and_brave/20100921/1285049653 の例をちょっとだけ洗練させてみた。
std::unique_ptr を使えばキャンセルできそう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment