Skip to content

Instantly share code, notes, and snippets.

@munro
Created November 22, 2011 23:47
Show Gist options
  • Save munro/1387457 to your computer and use it in GitHub Desktop.
Save munro/1387457 to your computer and use it in GitHub Desktop.
c++11 lambda
#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