/* File: lambda3.cpp One way to return and pass lambda expressions is the std::function facility */ #include // must #include to use std::function< > // #include using namespace std ; std::function< int(int) > makeAddXLambda(int x) { return [x](int n){ return n+x ; } ; // capture all by value } void apply( std::function< int(int) > f, int A[], int n) { for (int i=0 ; i < n ; i++) { A[i] = f( A[i] ) ; } } int main () { std::function< int(int) > add3 = makeAddXLambda(3) ; cout << "add3(5) = " << add3(5) << endl ; std::function< int(int) > add5 = makeAddXLambda(5) ; cout << "add5(8) = " << add5(8) << endl ; int B[10] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19} ; printf("Before:\n") ; for (int i=0 ; i < 10 ; i++) { cout << " B[" << i << "] = " << B[i] << endl ; } apply (add3, B, 10) ; printf("\n\nAfter calling apply(add3, B, 10):\n") ; for (int i=0 ; i < 10 ; i++) { cout << " B[" << i << "] = " << B[i] << endl ; } }