Lambda expressions in C + +

Source: Internet
Author: User

1. Basic form:

[Capture List] (parameter list)     {function Body}; The capture list and function body cannot be omitted, but the capture list can be empty, meaning that the simplest lambda expression is: []{};

The 2.LAMBDA expression is also called an anonymous function, which is a function without a function name, and a lambda expression in C + + represents a callable unit of code. We can interpret it as an unnamed inline function, paying particular attention to the type of return value that is determined by the return statement in the body of the function. Therefore, the Auto keyword is generally used to derive

3. Usage examples

int I=[]{return 1;} ();

I=1, front section, []{return 1;} is a lambda expression that omits an intermediate argument list, which can be understood as an anonymous inline function, followed by () representing a function call, assigning the return value of a function call to the variable i

And the following statement are equivalent:

Auto Func=[]{return 1;};/ The/LAMBDA expression is assigned to the Func pointer as an anonymous inline function pointer, noting that it can be converted to a function pointer when not captured, that is, the [] capture parameter list is empty

int I=func ();//func pointer Plus () indicates a function call, and then assigns the return value to the variable i

4. Use of the capture list:

int v=100;

Auto Func=[v]{return v;};/ The parameters in the/Capture list must be defined previously, called definition parameters, corresponding to the call parameters in the following ()

Cout<<func ();//Print 100

4.1 Capture lists can capture multiple variables

int a=100,b=200;

Auto Func=[a,b]{return a+b;};/ /simultaneous capture of two variables

int S=func ();

4.2 The Capture list has a reference and a value of two ways, by default, the value is passed, [a, b], the value, [&a,b],a is a reference, B is a value

int a=100,b=200;

Auto Func=[a,&b]{++b;return a+b;};/ /Because the capture parameter B is a reference, it can be changed, and the changed value will take effect outside the lambda expression, that is, after the statement, b=201, and A is a pass value, does not allow changes, ++a, or a=b; all errors.

int S=func ();

4.3 Use the mutable keyword to modify the capture parameters of a value, but the changed parameter value will not take effect outside the lambda expression

int p=100,q=200;

Auto Func=[&p,q] () Mutable{p++,q++;return p+q;};/ /Because the mutable keyword is used, it is possible to modify even the value of the capture parameter

int S=func ();

cout<<p<<endl;//p=101, passing references

cout<<q<<endl;//q=200, although mutable can be used to modify the capture parameters of a value, it does not take effect outside the lambda expression

4.4 Capture list can be used = or & to capture all variables, = means that the value,& means that the reference [=,&b] means that except for parameter B, it is a value, [=a,&] means that except for the argument A is passed, it is a reference, when the this pointer

int p=100,q=200;

Auto Func=[&]{q++,p+=2;return p+q;};/ /[&] indicates that all capture parameters are referenced and can be modified, especially if you need to use the this pointer.

Auto Func=[=]{return p+q;};/ /[=] means that all of the capture parameters are value, the default is this, non-modifiable, use this pointer when the VC platform does not error, the Linux platform error

int S=func ();

5. Lambda expression with call parameters

Auto add=[] (int a,int b) {return a+b;};/ /int A,int B is a parameter passed at the time of invocation, placed in ()

Auto Res=add (2,3);//res=5

The difference between capturing a parameter list and invoking a parameter list is that one is a parameter passed at the time of the definition, and the other is the argument passed at the time of the call.

6. Lambda expression with explicit return value: Use--

Auto add=[] (int v1,int v2)->int {return v1+v2;};/ /->int explicitly indicates that the return value type is int, and in general we use the Auto keyword to automatically derive

int S=add (10,20);

Cout<<s<<endl;//s=30

7. An empty lambda expression:

Auto func=[] () {};//is also legal, compile runs correctly

Lambda expressions in C + +

Related Article

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.