A brief analysis of the lambda expression of c++11 new features

Source: Internet
Author: User
Introduction to Lambda

Programmers who are familiar with Python should be no stranger to lambda. In a nutshell, a lambda is an anonymous, callable block of code. In the c++11 new standard, Lambda has the following format:

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

As you can see, he has four components:

1.capture list: Capture list

2.parameter List: Parameter lists

3.return Type: Return types

4.function Body: Execute code

Where the parameter list and return type can be ignored.

Here's a look at a few simple examples:

Auto F1 = [] {return 1;}; Auto F2 = [] () {return 2;}; COUT<<F1 () << ' \ t ' <<f2 () <<endl;

Capture List

A capture list in a lambda can capture a value or capture a reference.

Capture Value:

int test_data[] = {1, 5, 9, 7, 3, Th,, 17};int border = 8;auto F3 = [Border] (const int &i) {if (i > Border) cout& lt;<i<< ' \ t '; };for_each (Begin (Test_data), End (Test_data), F3);cout<<endl;

To capture a reference:

Auto F4 = [&border] (const int &i) {if (i > Border) cout<<i<< ' \ t ';}; border = 6;for_each (Begin (Test_data), End (Test_data), F4);cout<<endl;

As you can see from the output, the border that work in Lambda is the modified 6, confirming that the capture is indeed a reference.

It is important to note that when you capture a reference, you need to ensure that the reference is still valid when the lambda is called.

The capture list can also be implicitly captured by having the compiler determine which local variables need to be captured through the execution code of the lambda.

Implicit captures can capture a value, a reference, or a mixture of both:

char space = '; auto f5 = [=] (const int &i) {if (i > Border) cout<<i<< ' \ t ';}; Auto F6 = [&] (const int &i) {if (i > Border) cout<<i<< ' \ t ';}; Auto F7 = [&, space] (const int &i) {if (i > Border) cout<<i<<space;}; border = 0;for_each (Begin (Test_data), End (Test_data), F5), Cout<<endl;for_each (Begin (Test_data), End (Test_data ), f6); Cout<<endl;for_each (Begin (Test_data), End (Test_data), F7);cout<<endl;

The F7 used here can be read as "Other variables capture references in addition to space capture values."

Variable lambda

When a lambda needs to modify the value of a variable captured by a value, it needs to add the mutable keyword to the lambda. Otherwise there will be a compilation error.

Auto F8 = [&, space] (const int &i) mutable {if (i > Border) {cout<<i<<space; space= ' \ t ';}}; For_each (Begin (Test_data), End (Test_data), F8);cout<<endl;cout<<1<<space<<2<<endl;

As can be seen from the output, space in the lambda F8 value, after the first call, it was changed to Tab tab, but outside of the lambda, space is still a space.

return type

The return type of the lambda takes the form of a trailing return type. In general:

1.lambda if only a return statement is included, the compiler can infer its return type, at which point the specified return type may not be displayed;

2. Otherwise, the compiler assumes that the lambda returns void, and that a function that returns void cannot go back to any specific value, which in most cases is a contradiction, and therefore needs to display the specified return type.

However, the current g++ compiler is smarter than the actual test: for the 2nd, as long as the compiler can infer the return type of the function from the lambda function body, you do not need to explicitly specify the return type, for example:

Auto F9 = [] (const int i) {if (i% 3) return I * 3; else return i;}; Transform (Begin (Test_data), End (Test_data), Begin (Test_data), f9), border = 0;for_each (Begin (Test_data), End (Test_ Data), F6);cout<<endl;

There are multiple return statements in a lambda code block, and there are if/else statements, but the compiler can infer from the return statement that the return value should be an int type, so you can omit the trailing return type.

However, as in the following form, the return type must be explicitly specified because the compiler found an inconsistency when inferring the return type:

Auto F10 = [] (const int i)-double{if (i% 5) return I * 5.0; else return i;}; Transform (Begin (Test_data), End (Test_data), Begin (Test_data), F10), For_each (Begin (Test_data), End (Test_data), F6); cout<<endl;

Summarize

1.lambda expression form: [Capture list] (parameter list), return type {function Body}, where parameter list and return type can be omitted.

2. The capture list can capture a value [Val], or it can capture a reference [&ref].

3. The capture list can also be used to implicitly capture local variables, as well as capturing values [=] and capturing references [&] Both ways that can be mixed for the first time [& val] or [=, &ref].

4. When the lambda needs to modify the captured values, the mutable keyword needs to be added.

4. When a lambda cannot automatically infer the return value type, the designation needs to be displayed in the form of a trailing return type.

Above is the whole content of lambda expression of c++11 new feature, hope this article will help you to learn 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.