Support version

Lambda syntax is supported from c++ 11 onwards, so be careful to configure the compilation environment to the supported standard version. For example, compile with g++. Add -std=c++11.

The following is an example command

g++ -std=c++11 main.cpp
Copy the code

Of course you can choose a higher version to compile, after all forward compatibility, such as -std=c++2a.

Basic usage

Basic grammar

[function object arguments] (operator overloading function arguments) mutable or exception declaration -> return value type {function body}

A simple empty anonymous function

Even the simplest empty function has no return value.

void test(a) {
    const auto empty_call = []() {
        printf("empty call.\n");
    };
    empty_call(a); }Copy the code

Full value pass

Pass all variables visible to the lambda function as values. But it can’t be modified, so you can think of it as a constant.

void test(a) {
    int a = 2;
    int b = 5;
    const auto empty_call = [=]() {
        printf("a = %d b = %d\n", a, b); // a = 2 b = 5
    };
    empty_call(a); }Copy the code

Values are passed and arguments are variable

Pass in all variables within the visibility of the lambda expression as values. But modifiable, that is, variables.

Because it is passed by value, even if you change a variable within a lambda function, you only change a copy.

Note that the lambda function cannot be modified to const because the mutable modifier is used.

void test(a) {
    int a = 2;
    int b = 5;
    auto empty_call = [=]() mutable {
        a = 3;
        printf("a = %d b = %d\n", a, b); // a = 3 b = 5
    };
    empty_call(a);printf("a = %d b = %d\n", a, b); // a = 2 b = 5
}
Copy the code

In addition to the mutable modifier, lambda functions also support the exception modifier as usual.

All passed by reference

Pass all variables visible to the lambda function by reference. All references are modifiable if they are variables. Syntax is in [&].

Lambda functions passed by reference can be decorated with const without affecting the writability of the reference. Only mutating passed by value requires that const be enabled.

void test(a) {
    int a = 2;
    int b = 5;
    const auto empty_call = [&]() {
        a = 3;
        printf("a = %d b = %d\n", a, b); // a = 3 b = 5
    };
    empty_call(a);printf("a = %d b = %d\n", a, b); // a = 3 b = 5
}
Copy the code

Takes parameters and return values

The return value can be -> plus the return type.

void test(a) {
    const auto call = [&](int i) -> int {
        return i * i;
    };
    int a = 5;
    int b = call(a);
    printf("sqr(%d) = %d\n", a, b); // sqr(5) = 25
}
Copy the code

Specifying parameter passing

You can specify variables to be passed, and you can specify whether the variable is passed by value or by reference.

The specified operation can be compounded with =, which means that all variables except the specified one are passed by value, such as [=, & I].

void test(a) {
    int i = 5;
    int k = 5;
    // Specify I by reference and k by value
    auto call = [&i, k]() mutable {
        i++;
        k++;
    };
    call(a);printf("i = %d k = %d\n", i, k); // i = 6 k = 5
}
Copy the code
void test(a) {
    int i = 5;
    int k = 5;
    // Specify that I is referenced and all other variables are passed by value
    auto call = [=, &i]() mutable {
        i++;
        k++;
    };
    call(a);printf("i = %d k = %d\n", i, k); // i = 6 k = 5
}
Copy the code

lambdaWith the template (std::c++20A)

Subsequent c++ features have made lambda support for templates, but it’s a little cumbersome to write. Here are two simple examples.

void test(a) {
    const auto f = [] <size_t esize> () {
        printf("size = %lu\n", esize);
    };
    f.template operator(a)The < 16 >(a); // size = 16
    f.template operator(a)32 > <(a); // size = 32
}
Copy the code
void test(a) {
    const auto call = []<typename T>(T a) {
        std::cout << "output: " << a << std::endl;
    };
    call("hello, world."); // output: hello, world.
    call(1); // output: 1
    call(3.14); / / the output: 3.14
}
Copy the code