A brief analysis of c++11 new characteristics of lambda expression _c language

Source: Internet
Author: User

Introduction to Lambda

Programmers familiar with Python should be no stranger to the Lambda . In simple terms, alambda is an anonymous, callable block of code. In the new C++11 standard, theLambda has the following format:

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

As you can see, he has four components:

1.capture list: capturing lists

2.parameter list: parameter lists

3.return Type: return types

4.function Body: Executing code

Where the parameter list and return type can be ignored.

Below, look at a few simple examples:

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

Capture List

The capture list in a lambda can either capture the value or capture the reference.

Capture Value:

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

Capture 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;

The output shows that the border that work in theLambda is the modified 6, confirming that the capture is indeed a reference.

note that when capturing a reference, it is necessary to ensure that the reference is still valid when the lambda is invoked.

The capture list can also be implicitly captured in such a way that the compiler determines which local variables need to be captured through the execution code of the Lambda .

Implicit captures can capture values, references, or a mixture of both:

char space = ';
Auto F5 = [=] (const int &i) {if (i > Border) cout<<i<< ' \ t ';
Auto F6 = [n] (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;

Here the F7 uses a hybrid form that can be read as " other variables capture the reference except for the space capture value."

Variable lambda

When the Lambda needs to modify the value of the variable being captured by the 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 you can see from the output, the value of space in the lambda F8 is changed to Tab tab after the first call, but spaces are still whitespace outside the Lambda .

return type

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

1.Lambda If only the return statement is included, the compiler can infer the type of returns, 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 renege on any specific value, which in most cases is contradictory, so the specified return type needs to be displayed.

However, the current g++ compiler is smarter: for 2nd, as long as the compiler can infer the return type of a function from a lambda function Body, there is no need to explicitly specify the return type, for example:

Auto F9 = [] (const int i) {if (i% 3) return I * 3; else return i;};
Transform (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 the Lambda code block, and there are if/else statements, but the compiler can infer from the returns statement that the returned value should be an int type, you can omit the end return type.

However, as in the following form, the compiler must explicitly specify the return type because it finds inconsistencies when it infers the return type:

Auto F10 = [] (const int i)-> double
{if (i% 5) return I * 5.0; else return i;};
Transform (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. Capture list can capture values [val], or capture references[&ref]。

3. The capture list can also implicitly capture local variables, as well as capture values [=] and capture references [and], and can be mixed or captured in the first [&, val] case [=, &ref] .

4. When the Lambda needs to modify the captured value, you need to add the mutable keyword.

4. When a lambda cannot automatically infer a return value type, it needs to display the specified by the end return type.

The above is c++11 new characteristics of the lambda expression of the full content, I hope this article for everyone to learn C + + Help.

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.