/* File: lambda4.cpp If you want to modify a captured variable, you must use the keyword "mutable". Below the captured "count" variable is modified. */ #include // must #include to use std::function< > // #include using namespace std ; std::function< int(int) > makeAddXLambda(int x) { int count=0 ; count = 79 ; cout << "count = " << count << endl ; return [x,count](int n) mutable { cout << "( count = " << count << ") " ; return n+x+(count++) ; // modifies captured count } ; } int main () { std::function< int(int) > add3 = makeAddXLambda(3) ; cout << "add3(5): " << add3(5) << endl ; cout << "add3(5): " << add3(5) << endl ; cout << "add3(5): " << add3(5) << endl ; std::function< int(int) > add5 = makeAddXLambda(5) ; cout << "add5(8): " << add5(8) << endl ; cout << "add5(8): " << add5(8) << endl ; cout << "add5(8): " << add5(8) << endl ; }