C ++ 11/14 Learning (8) Lambda expressions and lambda expressions

Source: Internet
Author: User

C ++ 11/14 Learning (8) Lambda expressions and lambda expressions

Lambda expressions are one of the most important new features in C ++ 11. Similar to anonymous functions, it is used when a function is needed but it does not require effort to name it.
There are actually many such scenarios, so anonymous functions are almost the standard of modern programming languages.

Lambda expressions

The basic syntax of Lambda expressions is as follows:

[Capture list] (parameter list) mutable (optional) exception property-> return type {
// Function body
}

Capture list, which can be understood as a type of parameter.
By default, the internal function body of a lambda expression cannot use external variables of the function body. In this case, capturing the list can transfer external data.
Based on the passed behavior, the capture list is also divided into the following types:

1. Value capture

The premise of value capturing is that the variables can be copied. The captured variables are copied when the lambda expression is created, rather than when the call is used:

Voidlearn_lambda_func_1 (){
Intvalue_1 = 1;
Autocopy_value_1 = [value_1] {
Returnvalue_1;
};
Value_1 = 100;
Autostored_value_1 = copy_value_1 ();
// In this case, stored_value_1 = 1, while value_1 = 100.
// Because copy_value_1 saves a copy of value_1 during creation
}

2. Reference capture

The reference capture stores the reference, and the value changes:

Voidlearn_lambda_func_2 (){
Intvalue_2 = 1;
Autocopy_value_2 = [& value_2] {
Returnvalue_2;
};
Value_2 = 100;
Autostored_value_2 = copy_value_2 ();
// At this time, stored_value_2 = 100, value_1 = 100.
// Because copy_value_2 saves the reference
}

3. Implicit capture

Manual Writing of the capture list is sometimes very complicated, and this kind of mechanical work can be handled by the compiler.
You can write a & OR = file in the capture list to declare to the compiler using reference capture or value capture.
The most common four forms of capture list can be:
[] Empty capture list
[Name1, name2,...] capture a series of variables
[&] Reference capture, allowing the compiler to deduce the capture list by itself
[=] Value capture for the compiler to execute the derived Application List

4. Expression capture (C ++ 14)

You need to understand the right-value reference and smart pointer mentioned later.

The Value capture and reference capture mentioned above are all variables declared in the outer scope. Therefore, these capture methods capture the left value instead of the right value.
C ++ 14 allows the captured members to be initialized using any expression.
The declared capture variable type is determined based on the expression. The judgment method is essentially the same as that using auto:

# Include <iostream>
# Include <utility>
Intmain (){
Autoimportant = std: make_unique <int> (1 );
Autoadd = [v1 = 1, v2 = std: move (important)] (intx, inty)
-> Int {
Returnx + y + v1 + (* v2 );
};
Std: cout <add (3, 4) <std: endl;
Return 0;
}

In the above Code, important is an exclusive pointer and cannot be captured. In this case, we need to convert it to the right value and initialize it in the expression.

Generic Lambda (C ++ 14)

Lambda expressions are not common functions, so Lambda expressions cannot be templated.
This causes us a certain degree of trouble: The parameter table cannot be generalized, and the type of the parameter table must be specified.
Starting from C ++ 14, the form parameters of Lambda functions can use the auto keyword to generate a generic type in the sense:

Autoadd = [] (autox, autoy ){
Returnx + y;
};
Add (1, 2 );
Add (1.1, 2.2 );

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.