Skip to content

Instantly share code, notes, and snippets.

@BYVoid
Created October 10, 2012 11:39
Show Gist options
  • Save BYVoid/3865015 to your computer and use it in GitHub Desktop.
Save BYVoid/3865015 to your computer and use it in GitHub Desktop.
Closure in C++11
#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