Created
January 27, 2016 12:10
-
-
Save rc1/a9cb423c431d8c515d96 to your computer and use it in GitHub Desktop.
Variable capture / closures 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 <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