Skip to content

Instantly share code, notes, and snippets.

@tos-kamiya
Created August 7, 2012 12:02
Show Gist options
  • Save tos-kamiya/3284803 to your computer and use it in GitHub Desktop.
Save tos-kamiya/3284803 to your computer and use it in GitHub Desktop.
finally for C++0x
// reference: http://stackoverflow.com/questions/6167515/simulating-finally-block-in-c0x
#include <functional>
class finally
{
std::function<void (void)> const action;
finally(const finally&) = delete;
public:
finally(std::function<void (void)> a)
: action(a)
{}
~finally() { action(); }
};
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
{
finally clean_up([&]{
cout << "finally, this statement was executed." << endl;
});
cout << "do something, which may raise." << endl;
}
return 0;
}
// don't forget c++0x compiler, such as "g++ -std=c++0x"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment