Created
August 7, 2012 12:02
-
-
Save tos-kamiya/3284803 to your computer and use it in GitHub Desktop.
finally for C++0x
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
// 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