mercredi 1 juin 2022

Return lambda with capture from a function in c++11

The standard 5.1.2 6 says that there is a conversion function from a lambda expression without capture to the corresponding function pointer type. What about lambdas with capture? The following code compiles without warnings. Does this lead to undefined behavior?

std::function<void()> makeFucntion(int& parameter)
{
    return [&parameter]() // convert the lambda to std::function
    {
        cout << parameter;
    };
}

int var = 4;
auto foo = makeFucntion(var);
foo();

And if there is undefined behavior, is there another way to return a lambda expression with a capture from a function in c++11?

Aucun commentaire:

Enregistrer un commentaire