C + + Primer Note--LAMBDA expression

Source: Internet
Author: User

1. A lambda expression represents a callable unit of code that can be understood as an unnamed inline function, but unlike a function, a lambda expression may be defined inside a function. The form is as follows:

[Capture List] (parameter list), return type {function Body}

    • Capture list is a list of local variables defined in the function where the lambda is located (usually empty)
    • return type, parameter list and function body are the same as any normal function, representing return types, argument lists, and body functions.
    • The lambda must use the tail to return
    • We can ignore the argument list and return type, but must always contain the capture list and the function body
    • If the return type is omitted, lambda infers the return type from the function body.
    • If the function body of a lambda contains anything other than a single return statement, and no return type is specified, void is returned
    • The capture list is used only for local non-static variables, and lambda can use the local static variable directly and the name declared outside of its function.
void Test () {    int1;     int 2 ;     = [I, j] (intbaseintreturnbase + i + j;};    // I,j must be inside the capture list, where the return type int can actually be omitted    int k = f (0);    // The result of K is 3}

2. When a lambda is defined, the compiler generates a new (unnamed) class type corresponding to the lambda. By default, this class contains a data member for the variable that the lambda captures. Like any ordinary class of data members, Lambda's data members are also initialized when the lambda object is created.

3. Similar to the pass-through parameters, the assumption of value capture is that the variable can be copied, unlike the parameter, the value of the captured variable is copied at the time of the lambda creation, not at the time of the call.

void Test () {    int;     = [I] {return1;};    // The value of I is now copied.    int J = f ();}

4. When capturing a variable by reference, you must ensure that the variable exists when the lambda executes.

void Test () {    int;     = [&i] {return + +i;}    ; int j = f ();    // at this point I equalsone}

5. Write A & in the capture list or = You can tell the compiler whether we want to take a value capture or a reference capture, which is called an implicit capture.

void Test () {    int;     = [&] {return ++i;};    // using reference capture    int J = f ();    }


6. We can also mix value captures or reference captures. When blending is used, the display cannot be captured in the same way as implicit capture.

void Test () {    int;     int 9 ;     return i + j; };    // I use reference capture, others take value capture    int k = f ();    // k equals$}

7. By default, for a variable whose value is copied, the lambda does not change its value, and if we want to change the value of a captured variable, we must add mutable to the argument list.

void Test () {    int;     = [I] () {return ++i;};    // error, can not change the value    of I Auto F1 = [i] () mutable {return ++i;};    // correct }

The 8.bind function (in the header file functional) can be thought of as a generic function adapter that accepts a callable object and generates a new callable object to accommodate the original object's argument list. The general form of calling Bind is:

Auto newcallable = bind (callable, ARG);

voidTestintIintj) {Std::cout<< i-j <<Std::endl;} Auto Test0= Std::bind (Test,5,1); Test0 (); //Results 4Auto Test1= Std::bind (Test, std::p laceholders::_1,5, std::p laceholders::_2,1);//wrong, you can't write like that.test1 (); Auto Test2= Std::bind (Test, std::p laceholders::_1,5); Test2 (1);//Results-4Auto Test3=std::bind (Test, std::p laceholders::_1, std::p laceholders::_2); Test3 (3,2);//Results 1Auto Test4= Std::bind (Test, std::p laceholders::_2, std::p laceholders::_1);//parameter order can be re-sortedTest4 (3,2);//Results-1Auto Test5= Std::bind (Test,1, std::p laceholders::_1);//Note that this placeholder occupies the parameter position of the TEST5 () instead of the test ().TEST5 (5);//Results-4

The above code should be aware of the namespace, or the socket's bind function will produce two semantics.

9. The ref function should be used if we want to bind parameters that cannot be copied, or if we want to reference them.

void Test (intint  j) {    << i-j << Std::endl;} int 5  = std::bind (Test, std::ref1); Test0 ();     // Results 4


C + + Primer Note--LAMBDA expression

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.