C++11 lambda 函数 - 如何传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15853665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++11 lambda function - how to pass parameter
提问by Littlebitter
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?如何向他们传递参数?
回答by Andy Prowl
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
,那么您可以:
Use function pointers for non-capturing lambdas (capturing lambdas are notconvertible to function pointers). In the above case, thus, the following will also work:
#include <iostream> int main() { void (*f)(int) = [] (int i) { std::cout << "The answer is " << i; }; f(42); }
Use
std::function
(this is always possible, even if the lambda is capturing):#include <iostream> #include <functional> int main() { std::function<void(int)> f = [] (int i) { std::cout << "The answer is " << i; }; f(42); }
将函数指针用于非捕获 lambdas(捕获 lambdas不能转换为函数指针)。因此,在上述情况下,以下内容也将起作用:
#include <iostream> int main() { void (*f)(int) = [] (int i) { std::cout << "The answer is " << i; }; f(42); }
使用
std::function
(这总是可能的,即使 lambda 正在捕获):#include <iostream> #include <functional> int main() { std::function<void(int)> f = [] (int i) { std::cout << "The answer is " << i; }; f(42); }
回答by Littlebitter
auto lambda = [] (int a, int b) { return a + b; };
assert(lambda(1, 2) == 3);
回答by TonyK
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 ;