SoFunction
Updated on 2025-03-05

Detailed explanation of C++ Lambda expressions

Overview

Lambda expressions in C++11 are used to define and create anonymous function objects to simplify programming.LambdaThe syntax form is as follows:

[Capture list] (parameter) mutable or exception statement -> Return value type {Function body}
// Calculate the sum of two valuesauto func = [](int a, int b) -> int{return a+b;};
//When the return value type is OK, the return value can be ignoredauto func = [](int a, int b){return a + b;};
//Callint sum = func(1, 3);

Grammar analysis

Capture list

A Lambda expression is equivalent to a class, so the capture list is passed to the class members of this class. for example:

class Labmda
{
public:
    const int test;
    Labmda(int value):test(value)
    {
    }
public:
    int run(int a, int b)
    {
        return a + b + test;
    }
}
int main()
{
    int test = 10;
    auto func = Labmda(test);
    int sum = (1, 3);
}
//Writing using Lambda expressionint main()
{
    int test = 10;
    auto func = [test](int a, int b){return a + b + test;};
    int sum = func(1, 3);
}

The capture list has the following format:

Format describe
[] Without any parameters
[=] Local variables before Lambda expressions, including this in the class, are passed in value.
[&] Local variables before Lambda expressions, including this in the class, the variables are passed in reference form
[this] This of the class where the Lambda expression resides
[a] The value of the local variable a before the Lambda expression can also be passed in multiple values, such as [a, b]
[&a] Reference to local variable a before Lambda expression

Keyword Statement

Keyword declarations are generally rarely used, and they are not recommended to use them casually, and they can be ignored.

mutable

Valid when the capture list is passed as a value, after adding this keyword, the Lambda class member (with a const modifier) ​​can be modified. for example:

int test = 10;
//Compilation error, test member cannot be modifiedauto func = [test](int a, int b){test = 8; return a + b + test;}; 
//Compilation is normalauto func = [test](int a, int b)mutable {test = 8; return a + b + test;}; 

What you need to note here is: Lambda class memberstestAfter modification, the external will not be changedint testvalue.

exception

Exception declares an exception used to specify the exception thrown by a function. If an exception of an integer type is thrown, you can use throw(int)

Example

Capture list passed by value

int test = 10;
auto func = [=](int a, int b){return a + b + test;};
auto func2 = [test](int a, int b){return a + b + test;};
int sum = func(1, 3); //sumequal14

What needs to be noted here isfuncIn expressiontestThe value of the expression is only updated before:

int test = 10;
auto func = [=](int a, int b){return a + b + test;};
test = 5;
int sum = func(1, 3); //sumOr equal to14

Capture list passed by reference

int test = 10;
auto func = [&](int a, int b){test = 5; return a + b + test;};
auto func2 = [&test](int a, int b){test = 5; return a + b + test;};
int sum = func(1, 3); //sumequal9,testequal5

herefuncIn expressiontestThe value can be updated at any time:

int test = 10;
auto func = [&](int a, int b){return a + b + test;};
test = 5;
int sum = func(1, 3); //sumequal9,testequal5

Summarize

That’s all for this article. I hope it can help you and I hope you can pay more attention to more of my content!