lambda Expressions in C + + 11 are used to define and create anonymous function objects to simplify the programming effort. The syntax for Lambda is as follows:
[function Object parameter] (operator overloaded function parameter) mutable or exception declaration, return value type {function Body}
as you can see, lambda is mainly divided into five parts: [function Object parameter], (operator overloaded function parameter), mutable or exception declaration, return value type, {function body}. The following are described separately.
one, [function object parameter], identifies the beginning of a lambda, this part must exist, cannot be omitted. A function object parameter is a constructor that is passed to the compiler's automatically generated function object class. The function object parameter can only use local variables that are visible to the scope of the lambda in which it is defined (including the this of the class that the lambda resides in). Function object parameters have the following form:
1, empty. No function object parameters are used.
2, =. The body of the function can use all the visible local variables within the scope of the lambda's scope (including this of the class that the lambda is in), and it is the way the value is passed (equivalent to the compiler automatically passing all local variables for us by value).
3, &. The body of the function can use all the visible local variables within the scope of the lambda's scope (including this of the class that the lambda is in), and it is a reference delivery method (equivalent to the compiler automatically passing all local variables for us by reference).
4, this. The body of a function can use member variables in the same class as lambda.
5, A. The A is passed by value. When passing by value, the body of a function cannot modify the copy of a passed in because the function is const by default. To modify a copy of a passed in, you can add the mutable modifier.
6, &a. The A is passed by reference.
7, A, &b. The A is passed by value and B is passed by reference.
8, =,&a, &b. In addition to a and B are passed by reference, other parameters are passed by value.
9, &, A, B. Except for A and B are passed by value, other parameters are passed by reference.
second, (operator overloaded function parameter), identifies the parameter of the overloaded () operator, this part can be omitted when there is no parameter. Parameters can be passed by value (such as: (A, b)) and by reference (for example: (&A,&B)).
third, mutable or exception declaration, this part can be omitted. When you pass a function object parameter by value, you can modify the copy passed in by value (Note that you can modify the copy rather than the value itself) when you add the mutable modifier. The exception declaration is used to specify the exception that the function throws, such as an exception that throws an integer type, and you can use throw (int).
The return value type, which identifies the type of the function return value, can be omitted when the return value is void, or there is only one return in the function body where the compiler can infer the return value type automatically.
Five, {function Body}, identifies the implementation of the function, this part cannot be omitted, but the function body can be empty.
Here is a sample code to illustrate the various cases mentioned above, with simple comments in the code as a reference.
classctest{ Public: CTest (): M_ndata ( -) {NULL;}voidTestlambda () {vector<int>vcttemp; Vcttemp.push_back (1); Vcttemp.push_back (2); //no function object parameter, output: 1 2{for_each (Vcttemp.begin (), Vcttemp.end (), [] (intV) {cout << v <<Endl;}); } //Pass all visible local variables (including this) in the scope by value, output: { intA =Ten; For_each (Vcttemp.begin (), Vcttemp.end (), [=](intV) {cout << v+a <<Endl;}); } //Pass all visible local variables (including this) in the scope by reference, output: { intA =Ten; For_each (Vcttemp.begin (), Vcttemp.end (), [&] (intV) mutable{cout << v+a << Endl; a++; }); cout<< a <<Endl; } //Pass local variable A in value, output: Ten { intA =Ten; For_each (Vcttemp.begin (), Vcttemp.end (), [a] (intV) mutable{cout << v+a << Endl; a++; }); cout<< a <<Endl; } //Pass local variable A as a reference, output: one by one { intA =Ten; For_each (Vcttemp.begin (), Vcttemp.end (), [&a] (intV) {cout << v+a << Endl; a++; }); cout<< a <<Endl; } //pass this, output:{for_each (Vcttemp.begin (), Vcttemp.end (), [ This](intV) {cout << v+m_ndata <<Endl;}); } //except B by reference, others are passed by value, output: { intA =Ten; intb = the; For_each (Vcttemp.begin (), Vcttemp.end (), [=, &b] (intV) {cout << v+a << Endl; b++; }); cout<< b <<Endl; } //operator overload function arguments are passed by reference, output: 2 3{for_each (Vcttemp.begin (), Vcttemp.end (), [] (int&V) {v++; }); For_each (Vcttemp.begin (), Vcttemp.end (), [] (intV) {cout << v <<Endl;}); } //an empty lambda expression { [](){}(); []{}(); } }Private: intM_ndata;};
Summary of c++11 lambda expression Summary