C ++ 11 lambda函数 - 如何传递参数

时间:2022-11-01 22:05:58

I used lambda function to pass it to std::condition_variable wait() function, but that is not the case. I use lambda functions that don't receive any parameters, and everything is absolutely clear for me. But I totally don't understand how is used lamdba function that have parameters list. Show lambda with parameters are used? How to pass parameters to them?

我使用lambda函数将它传递给std :: condition_variable wait()函数,但事实并非如此。我使用没有接收任何参数的lambda函数,对我来说一切都是绝对清楚的。但我完全不明白如何使用具有参数列表的lamdba函数。显示带参数的lambda?如何将参数传递给他们?

3 个解决方案

#1


28  

Show lambda with parameters are used? How to pass parameters to them?

显示带参数的lambda?如何将参数传递给他们?

It works exactly like with any other type of callable object:

它的工作方式与任何其他类型的可调用对象完全相同:

#include <iostream>

int main()
{
    auto l = [] (int i) { std::cout << "The answer is " << i; };
    l(42);
}

Also notice, that you do not need to store a lambda in a variable in order to invoke it. The following is an alternative way to rewrite the above program:

另请注意,您不需要在变量中存储lambda以便调用它。以下是重写上述程序的另一种方法:

#include <iostream>

int main()
{
    [] (int i) { std::cout << "The answer is " << i; } (42);
    //                                                 ^^^^
    //                                 Invoked immediately!
}

The type of a lambda function (the so-called "lambda closure") is defined by the compiler, and is a functor with a call operator whose signature is the one you specify when defining the lambda. Therefore, you call a lambda exactly as you would call a functor (i.e. exactly as you would call a function - or any callable object).

lambda函数的类型(所谓的“lambda闭包”)由编译器定义,并且是一个带有调用操作符的仿函数,其签名是您在定义lambda时指定的签名。因此,您可以像调用函子一样调用lambda(就像调用函数一样 - 或任何可调用对象)。

Thus, if you want to assign a lambda to an object, the best practice is to let the compiler deduce its type by using auto. If you do not want or cannot use auto, then you may:

因此,如果要将lambda分配给对象,最佳做法是让编译器使用auto来推断其类型。如果您不想或不能使用auto,那么您可以:

  1. Use function pointers for non-capturing lambdas (capturing lambdas are not convertible to function pointers). In the above case, thus, the following will also work:

    使用函数指针来捕获非捕获lambda(捕获lambda不能转换为函数指针)。因此,在上述情况下,以下内容也适用:

    #include <iostream>
    
    int main()
    {
        void (*f)(int) = [] (int i) { std::cout << "The answer is " << i; };
        f(42);
    }
    
  2. Use std::function (this is always possible, even if the lambda is capturing):

    使用std :: function(这总是可行的,即使lambda正在捕获):

    #include <iostream>
    #include <functional>
    
    int main()
    {
        std::function<void(int)> f = [] (int i) 
                                   { std::cout << "The answer is " << i; };
        f(42);
    }
    

#2


4  

auto lambda = [] (int a, int b) { return a + b; };
assert(lambda(1, 2) == 3);

#3


1  

You don't even need a variable to hold your lambda -- you can call it directly:

你甚至不需要一个变量来保存你的lambda - 你可以直接调用它:

std::cout << [](int n) { return n + 1 ; } (99) << std::endl ;

#1


28  

Show lambda with parameters are used? How to pass parameters to them?

显示带参数的lambda?如何将参数传递给他们?

It works exactly like with any other type of callable object:

它的工作方式与任何其他类型的可调用对象完全相同:

#include <iostream>

int main()
{
    auto l = [] (int i) { std::cout << "The answer is " << i; };
    l(42);
}

Also notice, that you do not need to store a lambda in a variable in order to invoke it. The following is an alternative way to rewrite the above program:

另请注意,您不需要在变量中存储lambda以便调用它。以下是重写上述程序的另一种方法:

#include <iostream>

int main()
{
    [] (int i) { std::cout << "The answer is " << i; } (42);
    //                                                 ^^^^
    //                                 Invoked immediately!
}

The type of a lambda function (the so-called "lambda closure") is defined by the compiler, and is a functor with a call operator whose signature is the one you specify when defining the lambda. Therefore, you call a lambda exactly as you would call a functor (i.e. exactly as you would call a function - or any callable object).

lambda函数的类型(所谓的“lambda闭包”)由编译器定义,并且是一个带有调用操作符的仿函数,其签名是您在定义lambda时指定的签名。因此,您可以像调用函子一样调用lambda(就像调用函数一样 - 或任何可调用对象)。

Thus, if you want to assign a lambda to an object, the best practice is to let the compiler deduce its type by using auto. If you do not want or cannot use auto, then you may:

因此,如果要将lambda分配给对象,最佳做法是让编译器使用auto来推断其类型。如果您不想或不能使用auto,那么您可以:

  1. Use function pointers for non-capturing lambdas (capturing lambdas are not convertible to function pointers). In the above case, thus, the following will also work:

    使用函数指针来捕获非捕获lambda(捕获lambda不能转换为函数指针)。因此,在上述情况下,以下内容也适用:

    #include <iostream>
    
    int main()
    {
        void (*f)(int) = [] (int i) { std::cout << "The answer is " << i; };
        f(42);
    }
    
  2. Use std::function (this is always possible, even if the lambda is capturing):

    使用std :: function(这总是可行的,即使lambda正在捕获):

    #include <iostream>
    #include <functional>
    
    int main()
    {
        std::function<void(int)> f = [] (int i) 
                                   { std::cout << "The answer is " << i; };
        f(42);
    }
    

#2


4  

auto lambda = [] (int a, int b) { return a + b; };
assert(lambda(1, 2) == 3);

#3


1  

You don't even need a variable to hold your lambda -- you can call it directly:

你甚至不需要一个变量来保存你的lambda - 你可以直接调用它:

std::cout << [](int n) { return n + 1 ; } (99) << std::endl ;