Created
November 22, 2011 23:47
-
-
Save munro/1387457 to your computer and use it in GitHub Desktop.
c++11 lambda
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 <iostream> | |
#include <functional> | |
#include <memory> | |
using namespace std; | |
std::function<void()> createCounter() { | |
std::shared_ptr<int> count(new int(0)); | |
return [count](){ | |
(*count) += 1; | |
cout << "count = " << (*count) << "\n"; | |
}; | |
} | |
int main(int argc, char** argv) { | |
auto counter = createCounter(); | |
counter(); | |
counter(); | |
counter(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment