Created
October 10, 2012 11:39
-
-
Save BYVoid/3865015 to your computer and use it in GitHub Desktop.
Closure in C++11
This file contains hidden or 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 <functional> | |
using namespace std; | |
function<void()> closure() { | |
int a = 0; | |
auto func = [a]() mutable { | |
a += 1; | |
cout << a << endl; | |
}; | |
return func; | |
} | |
int main() { | |
auto f = closure(); | |
f(); //1 | |
f(); //2 | |
f(); //3 | |
f(); //4 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment