C + + lambda functions

Source: Internet
Author: User

"1" lambda expression syntax definition

The syntax of a lambda expression is defined as follows:

[Capture] (parameters) mutable->return-type {statement};

  (1) [Capture]: Capture list. The capture list always appears at the beginning of the lambda function. Essentially, [] is a lambda balloon (that is, a unique marker)

The compiler determines whether the next code is a lambda function based on the

The snap list captures variables in the context for use by the lambda function

A snap list consists of multiple snap items separated by commas, and the catch list generally has the following forms:

<1> [var] indicates how value is passed to capture Var

<2> [=] indicates that the value is passed in a variable that captures all parent scopes (including the This pointer)

<3> [&var] represents a reference passing capture variable var

<4> [&] represents a variable that refers to passing all parent scopes (including the This pointer)

<5> [This] indicates how the value is passed to capture the current this pointer

<6> [=,&a,&b] means capturing variables A and B in a reference pass, and capturing all other variables in a value-passing manner

<7> [&,a,this] means capturing a and this as a value pass, and capturing all other variables in a reference pass

Note: A parent scope refers to a statement block that contains a lambda function

Also, it is important to note that the catch list does not allow variables to be passed repeatedly. The following example is a typical repetition that causes a compilation error:

[=, a] here = All variables have been captured in a value-passing way, then catching a is duplicates

[&,&this] Here & has captured all variables in a reference pass, then capturing this is a duplicate

  (2) (parameters): Parameter list. is consistent with the argument list of the normal function. If parameter passing is not required, you can omit it together with parentheses ()

  (3) mutable:mutable modifier. by default, a lambda function is always a const function, and mutable can cancel its constant

The argument list cannot be omitted when the modifier is used (even if the argument is empty)

  (4)->return-type: return type. declares the return type of a function in the form of a trace return type.

As a convenience, you can omit the return value when you don't need it.

In addition, you can omit this part if the return type is explicit, and let the compiler deduce the return type

  (5) {statement}: function body. the content is the same as a normal function, but you can use all the captured variables in addition to the parameters you can use

In the definition of a lambda function, both the parameter list and the return type are optional parts, and the catch list and function body may be empty

So, in extreme cases, the simplest lambda function in c++11 only needs to be declared as:

[]{};

You can do it. But obviously, such a lambda function can't do anything.

"2" lambda function sample code

Example code 1:

1#include <iostream>2 using namespacestd;3 4 voidMain ()5 {6     intA = -, B =Ten;7 8Auto Totalab = [] (intXintY)int{returnX +y;};9     intAADDB =Totalab (A, b);Tencout <<"AADDB:"<< AADDB <<Endl; One  AAuto TotalAB2 = [A, &b] ()int{returnA +b;}; -     intAADDB2 =totalAB2 (); -cout <<"AADDB2:"<< AADDB2 <<Endl; the  -Auto TotalAB3 = [=] ()int{returnA +b;}; -     intAADDB3 =totalAB3 (); -cout <<"AADDB3:"<< aAddb3 <<Endl; +  -[]{};//the simplest lambda function +[=] {returnA + b; };//The parameter list and return type are omitted, and the return type is inferred by the compiler as int AAuto FUN1 = [&] (intc) {B = a + C;};//return type omitted, no return value atAuto Fun2 = [=, &b] (intc)int{returnB + = a + C; };//a lambda function with all the parts intact. -cout <<"fun2 (+):"<< fun2 ( -) <<Endl; - } - //Result: - /* - aaddb:30 in aaddb2:30 - aaddb3:30 to fun2 (+): + */

The above code is for learning reference only

"3" lambda function summary

Use of functions

Good good Study, day.

C + + lambda functions

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.