Lambda expressions for new features of C++11

Source: Internet
Author: User

After writing C #, I feel that the lambda expression in C # is used in conjunction with delegate, and this mechanism is very cool. C++11 also has lambda expressions, which have small differences in form. The form is as follows:

C #:(input parameters) = {statement;}

C++:[capture list] (parameter list), return type {statement;}

The C++LAMBDA expression is divided into 4 parts. where parameter list and return type can be omitted under certain circumstances.

First, capture list

Capture list is the role of capturing the local variables of the function where lambda is located. the types captured can be categorized as value captures, reference captures, and implicit captures.

Value capture:
1 void Fun () 2 {3     int a = 14     return  A;}; 5     0 ; 6     Auto R =7     cout << R << Endl; 8 }

The result is 1. As with function value passing, the V1 captured by the line 4 lambda expression is a copy of the local variable A in the fun function, so line 5 changes a without affecting the A in the lambda expression.

Reference capture:
 1  void   Fun ()  2  3  int  a = 1  ; 4  auto f = [&a ] {return   A;};  5  a = 0;  6  auto R = F ();  7  cout << r << Endl;  8 } 

The result is 0, and line 4 captures the A object itself. It is worth mentioning, however, that a reference capture is used to ensure that the referenced variable is present when the lambda expression is being worked on.

Implicit capture:

The implicit way of capturing is that the list of captures can be replaced with ' = ' and ' & ', so that the compiler implicitly infers that you are using that variable, and then the two characters represent the captured type ' = ' for the value capture, ' & ' is the reference capture. Using implicit capture, line 4 of the above two pieces of code can be represented as:

Auto F = [=] {return A;}

Auto F = [&] {return A;}

If you need to, you can also choose to mix these several ways:

  

1voidFun ()2 {3     intA =1;4     intb =2;5     intc =3;6Auto F = [&,a]{cout <<"A ="<< a <<"b="<< b <<"c="<<c;};7a++;8 b++;9 C + +;Ten f (); One}

Output result: A = 1 b=3 c=4

It is not difficult to discover how the B,c are used to capture the way in which a reference is captured and the value that a takes. Implicit capture works like this: For a capture list such as [' = ' or ' & ', capture list or null], the parameter is not necessary, and is equivalent to a special type of the previous case (such as template specificity). That is, if ' = ' is used in the previous location, capture list can select those local variables that need to be referenced for capture or empty (but not the local variables that can also be captured by the value). ' & '.

Second, parameter list

Broadly speaking, parameter list usage is similar to normal functions. C++11 Standard Provisions lambda expressions can not have default parameters, but I test the following code in g++ 4.8 and vs2013 respectively, g++ can pass and give the desired result, vs2013 error that the lambda expression can not have default parameters. Obviously g++ has extended the c++11, and we should strictly abide by the standards for the portability of the code.

Specific Reference C++11 Documentation: 5.1.2.5:

The closure type for a lambda-expression have a public inline function call operator (13.5.4) whose param-eters and return Type is described by the Lambda-expression ' s parameter-declaration-clause and Trailing-return-type respectively. This function, call operator are declared const (9.3.1) if and only if the lambda-expression ' s Parameter-declaration-clause Is isn't followed by mutable. It is neither virtual nor declared volatile. Default Arguments (8.3.6) shallnot being speci?ed in the parameter-declaration-clause of a lambda-declarator. Anyexception-speci?cationspeci?edonalambda-expressionappliestothecorrespondingfunction call operator. An attribute-speci?er-seq in a lambda-declarator appertains to the type of the corresponding function call operator.

1 intFun ()2 {3Auto F = [] (stringS1,stringS2) {cout << s1 <<S2;};4F"Hello","world\n");5Auto F2 = [] (stringS1,stringS2="hahah\n") {cout << s1 <<S2;};6F2 ("Eric");7}

Third, return type

The return type in Lambda-declarator must also be the c++11 return type (trailing return). And can be omitted, can be given to the compiler to infer. (vs2013 and g++ can infer the return type if the lambda body uses if, of course, each If-else branch should return a uniform type, otherwise the declaration of the positional return type needs to be added).

Iv. mutable

Adding a keyword mutable between the parameter list and return type of the lambda expression means that the captured value can be changed in {}, and the value captured by default is not allowed to change (but the captured reference can be changed). Perhaps for efficiency, without mutable the previous value of the capture, can be less open up some memory, only after adding mutable after the actual memory for the captured value allocation.

Lambda expressions for new features of C++11

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.