Skip to content

Instantly share code, notes, and snippets.

@rc1
Created January 27, 2016 12:10
Show Gist options
  • Select an option

  • Save rc1/a9cb423c431d8c515d96 to your computer and use it in GitHub Desktop.

Select an option

Save rc1/a9cb423c431d8c515d96 to your computer and use it in GitHub Desktop.
Variable capture / closures in C++11
#include <functional>
#include <string>
#include <vector>
int main () {
std::string name;
std::vector<std::function<void(void)>> fns;
name = "adam";
fns.push_back( [ name ] () { printf( "hello %s\n", name.c_str() ); } );
name = "joe";
fns.push_back( [ name ] () { printf( "hello %s\n", name.c_str() ); } );
for ( auto &fn : fns ) {
fn();
}
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment