New feature of C++11: lambda expression

Keywords: C++

Lamda expression

Lambda expressions in C++ 11 are used to define and create anonymous function objects to simplify programming.

A complete Lambda expression is as follows:

[=] (int x, int y) mutable -> int { int z = x + y; return z; }

[Function object parameters] (Operator overloading function parameters) mutable or exception statement -> return type {Function body}

As you can see, Lambda is mainly divided into five parts:

[Function object parameters]
(Operator overloading function parameters)
mutable or exception statement
-> return type
{Function body}

Various parameters

1. [function object parameters]

Identify the beginning of a Lambda expression. This part must exist and cannot be omitted.

Only input parameters can be seen inside the Lambda function. If you want to access variables outside the function, you need to use [function object parameter].

Function object parameters can only use local variables that are visible within the scope of Lambda when Lambda is defined (including this of Lambda's class).

Null: no function object arguments.
= : Function body can be used Lambda All visible local variables (including Lambda Class this)
    And it is the value passing mode (equivalent to that the compiler automatically passes all local variables by value for us).
& : Function body can be used Lambda All visible local variables (including Lambda Class this)
    And it is a reference passing method (equivalent to that the compiler automatically passes all local variables for us by reference).
this : Function body can be used Lambda Member variables in the class.
a : take a Pass by value. When passing by value, the passed in function body cannot be modified a Copy of,
    Because by default, the function is const To modify the incoming copy, you can add mutable Modifier.
&a :  take a Pass by reference.
a,&b : take a Pass by value, b Pass by reference.
=,&a,&b : except a and b Except passing by reference, other parameters are passed by value.
&,a,b :  except a and b Except passing by value, other parameters are passed by reference.

Function object parameters are passed to the constructor of the function object class automatically generated by the compiler.

2. (operator overloads function parameters)

Declare the arguments to the Lambda function.

Identify the parameters of the overloaded () operator. If there are no parameters, this part can be omitted.

Parameters can be passed by value (such as: (a, b)) and by reference (such as (& A, & B)).

3. mutable or exception declaration

When passing function object parameters by value (that is, the first part of external variables, not function input parameters), the default is const. After adding the mutable modifier, you can modify the passed in copy (note that you can modify the copy, not the value itself).

The exception declaration is used to specify the exceptions thrown by the function, such as throwing integer type exceptions. throw(int) can be used.

This part can be omitted.

4. - > return value type

Identifies the type of the return value of the function

This part can be omitted when the return value is void or there is only one return in the function body (at this time, the compiler can automatically infer the return value type).

5. {function body}

Identify the implementation of the function. This part cannot be omitted, but the function body can be empty.

Several examples

[] (int x, int y) { return x + y; } // Implicit return type
[] (int& x) { ++x;  } // There is no return statement. The return type of the - > lambda function is' void '
[] () { ++global_x;  } // No parameters, only a global variable is accessed
[] { ++global_x; } // Same as the previous one, the (operator overloaded function parameter) is omitted

The specified return type can be displayed as follows:  

[] (int x, int y) -> int { int z = x + y; return z; }

In this example, a temporary variable z is created to store the intermediate value. Like ordinary functions, this intermediate value will not be saved until the next call. Nothing is returned
Lambda functions can omit the return type without using the - > void form.

Lambda functions can reference variables declared outside it. The collection of these variables is called a closure. Closure is defined in the square of lambda expression declaration
This mechanism allows these variables to be captured by value or by reference, as shown in the following example:

std::vector<int> some_list;
int total = 0;
for (int i = 0; i < 5; ++i) some_list.push_back(i);
std::for_each(begin(some_list), end(some_list), [&total](int x)
{
    total += x;
});

  This example calculates the sum of all elements in the list. The variable total is saved as part of the Lambda function closure. Because it is a reference to the stack variable (local variable) total, its value can be changed.

Posted by jeturman on Tue, 12 Oct 2021 18:44:31 -0700