[Original] C ++ 11 lambda expressions

Source: Internet
Author: User

Lambda expressions in C ++ 11 are used to define and create anonymous function objects to simplify programming. Lambda Syntax:
[Function object parameters] (operator overload function parameters) mutable or exception Declaration-> return value type {function body}
As you can see, lambda is mainly divided into five parts: [function object parameters], (operator overload function parameters), mutable or exception declaration,-> return value type, {function body }. The following is an introduction.
1. [function object parameters] to identify the beginning of a lambda. This part must exist and cannot be omitted. Function object parameters are passed to the constructor of the function object class automatically generated by the compiler. Function object parameters can only use local variables that are visible within the scope of Lambda when Lambda is defined (including this of the class where Lambda is located ). Function object parameters take the following form:
1. Empty. No function object parameters are used.
2. =. The function body can use all visible local variables (including this of the lambda class) within the scope of the lambda function ), and it is the value transfer method (equivalent to the compiler automatically passing all local variables by value ).
3 ,&. The function body can use all visible local variables (including this of the lambda class) within the scope of the lambda function ), and it is the reference transfer method (equivalent to the compiler automatically passing all local variables for us by reference ).
4. This. The function body can use the member variables in the class where Lambda is located.
5.. Pass a by value. When passing by value, the function body cannot modify the copy of a passed in, because the function is const by default. To modify the copy of a passed in, you can add a mutable modifier.
6. &. Pass a by reference.
7. A, & B. Pass a by value, and B by reference.
8, =, & A, & B. Except A and B are passed by reference, other parameters are passed by value.
9. &, A, and B. Except A and B are passed by value, other parameters are passed by reference.
II. (operator overload function parameters): identifies the parameters of the overloaded () operator. If no parameter exists, this part can be omitted. Parameters can be passed by value (for example, (a, B) or by reference (for example, (& A, & B.
Iii. mutable or exception declaration. This part can be omitted. When passing function object parameters by value with the mutable modifier, you can modify the copy passed by value (note that the copy can be modified, rather than the value itself ). The exception declaration is used to specify the exceptions thrown by the function. For example, throw (INT) can be used to throw an integer exception ).
4.-> return value type, which identifies the type of the function return value. When the return value is void or the function body contains only one return field (in this case, the compiler can automatically infer the return value type, this part can be omitted.
5. {function body}: indicates the implementation of the function. This part cannot be omitted, but the function body can be empty.
The following is an example. Code To demonstrate the various situations mentioned above. The Code has simple comments for reference.

Class Ctest
{
Public :
Ctest (): m_ndata ( 20 ) {NULL ;}
Void Testlambda ()
{
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 in a value manner, and output: 11 12
{
IntA =10;
For_each (vcttemp. Begin (), vcttemp. End (), [=] (IntV) {cout <v + A <Endl ;});
}

// transmit all visible local variables (including this) in the scope in reference mode, and output: 11 13 12
{
int A = 10 ;
for_each (vcttemp. begin (), vcttemp. end (), [&] ( int V) mutable {cout cout }

// pass local variable A by value, output: 11 13 10
{
int A = 10 ;
for_each (vcttemp. begin (), vcttemp. end (), [a] ( int V) mutable {cout cout }

// pass local variable A as a reference, output: 11 13 12
{
int A = 10 ;
for_each (vcttemp. begin (), vcttemp. end (), [& A] ( int V) {cout cout }

// pass this and output: 21 22
{
for_each (vcttemp. begin (), vcttemp. end (), [ This ] ( int V) {cout }

// except that B is passed by reference, others are passed by value. Output: 11 12 17
{
int A = 10 ;
int B = 15 ;
for_each (vcttemp. begin (), vcttemp. end (), [=, & B] ( int V) {cout cout }

//Operator overload function parameters 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 ;});
}

//Empty Lambda expression
{
[] () {} ();
[] {} ();
}
}

Private:
 IntM_ndata;
};

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.