A simple code
I am not a literary person, for the history of lambda, and the source of Lambda and C + +, I am not very familiar with the technical people, pay attention to take the code to say things.
#include <iostream>using namespace std; int main () { int a = 1; int b = 2; Auto Func = [=, &b] (int c)->int {return b + = a + C;}; return 0;}
Basic syntax
To put it simply, a lambda function is a function whose syntax is defined as follows:
[Capture] (parameters) mutable,return-type{statement}
1.[capture]: Capture list. The capture list always appears at the beginning of the lambda function. In fact, [] is a lambda balloon. The compiler determines whether the next code is a lambda function according to the balloon. The capture list captures variables in the context for use by the lambda function;
2. (parameters): Parameter list. is consistent with the argument list of the normal function. If parameter passing is not required, it can be omitted together with parentheses "()";
The 3.mutable:mutable modifier. By default, a lambda function is always a const function, and mutable can cancel its constant behavior. When the modifier is used, the argument list cannot be omitted (even if the argument is empty);
4.->return-type: return type. Declares the return type of a function in the form of a trace return type. We can also omit the symbol "--" when no return value is required. In addition, if the return type is explicit, you can omit the part and let the compiler deduce the return type;
5.{statement}: Function body. The content is the same as a normal function, but you can use all the captured variables in addition to the parameters you can use.
The biggest difference from normal functions is that, in addition to using parameters, lambda functions can access data in some contexts through a capture list. Specifically, the capture list describes what data in the context can be used by lambda, and how it is used (in the way that values are passed or referenced). Syntactically, the "[]" is included in the capture list, and the snap list consists of multiple snap items, separated by commas. The capture list has several forms:
1.[var] means that the value is passed to capture the variable var;
2.[=] indicates that the value is passed in a variable that captures all parent scopes (including this);
3.[&var] Indicates that the reference passes the catch variable var;
4.[&] represents a variable (including this) that captures all parent scopes by means of a reference pass;
5.[this] Indicates how the value is passed to capture the current this pointer.
It mentions a parent scope, which is a block of statements that contains a lambda function, saying that popular points are "{}" blocks of code that contain lambda. The above snap list can also be combined, for example:
1.[=,&A,&B] means that the variables A and B are captured in a reference pass, and all other variables are captured in a value-passing manner;
2.[&,a,this] means that the variable A and this are captured in a value pass, and the reference is passed to catch all other variables.
It is worth noting, however, that the catch list does not allow variables to be passed repeatedly. The following examples are typical repetitions that lead to compile-time errors. For example:
3.[=,A] All the variables have been captured in a value-passing way, but a is repeatedly captured and an error is made;
4.[&,&this] Here & has captured all variables in a reference pass, and capturing this is also a repetition.
About Lambda, those wonderful things.
#include <iostream>using namespacestd; intMain () {intj =Ten; Auto By_val_lambda= [=]{returnJ +1; }; Auto By_ref_lambda= [&]{returnJ +1; }; cout<<"By_val_lambda:"<<by_val_lambda () <<Endl; cout<<"By_ref_lambda:"<<by_ref_lambda () <<Endl; ++J; cout<<"By_val_lambda:"<<by_val_lambda () <<Endl; cout<<"By_ref_lambda:"<<by_ref_lambda () <<Endl; return 0; }
The program output results are as follows:
one byone
Do you have any idea??? Then why is this? Why is a third output not 12?
In By_val_lambda, J is treated as a constant that, once initialized, is not changed (it can be thought of as a constant with the same name as the J in the parent scope), while in By_ref_lambda, J still uses the value from the parent scope. So, when using a lambda function, if the value that needs to be captured becomes a constant of a lambda function, we usually use the method of passing by value, and conversely, if you need to capture a value that becomes a variable that the lambda function is running, you should catch it by reference.
Lambda expressions in C + +