Created
February 26, 2013 01:09
-
-
Save anonymous/5034882 to your computer and use it in GitHub Desktop.
Fail closure
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 <memory> | |
#include <iostream> | |
using namespace std; | |
typedef function<void(int)> func; | |
shared_ptr<func> create_lambda() { | |
return make_shared<func>([]() { | |
int tmp; | |
return [&tmp](int c) { | |
cout << tmp++ << endl; | |
}; | |
}()); | |
} | |
int main(void) { | |
shared_ptr<func> normal(create_lambda()); | |
(*normal)(1); | |
(*normal)(2); | |
(*normal)(5); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment