Lambda overview in C ++ 11

Source: Internet
Author: User

Although I have no interest in C ++ 11, C ++ 03 already has many complicated technologies. I tried to apply the Complex C ++ technology I learned to the project, but I found it has brought a lot of burden to other members of the team. In fact, it will bring a lot of burden to you in the future. Especially for the application of template, the template code looks like a hacker in appearance, even if you know Lisp and disagree with the parentheses in Lisp, however, I still feel uneasy when I see the parentheses on the screen.

However, after learning a little about some features of C ++ 11, it is quite interesting theoretically. I feel that C ++ 11 has added many functional language features and ideas, which is the biggest reason I am interested in. Let's take a look at lambda in C ++ 11 today.

In C ++ 03, when using STL containers or classes I write myself, there is a need for traversal. You can write a functor to upload it, however, this functor is ugly. Because you need to locally define a struct, reload operator (), and if the operator () depends on the context information of the functor during construction, You have to insert several members into the struct, of course, we have to make the parameters of the constructor longer and longer. Finally, the code block that contains your functor usage and struct definition has become very strange in its code format. If you use it as often as I do, you will be deeply touched.

Then, C ++ 11 came, and lambda in C ++ 11, as far as I am concerned, its syntax is still very modern. Let's take a look at its syntax format (captured from N2550 ):

Lambda-introducer lambda-parameter-declaration compound-statement
Capture-default, capture-list
Capture-list, capture
& Identifier
(Lambda-parameter-declaration-list) exception-specification lambda-return-type-clause
Lambda-parameter, lambda-parameter-declaration-list
Decl-specifier-seq declarator
-> Type-id
The translation is roughly like this:

[Capture] (parameter) spec-> return-type {body}
Capture is the list of variables in the scope of the lambda definition that can be accessed in the lambda implementation, just like the upvalue in Lua. In fact, I think this is the most convenient place for lambda programmers. Generally, functional languages do not need to display the declaration list and directly reference these variables. The following sections are easy to understand. parameter is the list of parameters used to call lambda. return-type is the type of return Value of lambda, and body is the implementation of lambda. As for spec, it mainly specifies the permission for exceptions and variables in capture in the body. Example:

Vector <int> ints;
Ints. push_back (99 );
Ints. push_back (100 );
Ints. push_back (101 );
Int threhold = 100;
Int sum = 0;
For_each (ints. begin (), ints. end (),
[Threhold, & sum] (int v ){
If (v> = threhold) ++ sum;
Printf ("% d \ n", sum );
Capture uses threhold and sum, but threhold only uses its value, while sum uses its reference. The result shows that the value of sum is changed in lambda.

C ++ 11 is being supported by more and more compilers. Here is a table listing the support of each feature of C ++ 11 on each compiler. It is for reference only (the above sample code is tested in vs2010, that is, MSVC10.0 ).

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: 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.