如何创建一个可变的通用lambda? [英] How to create a variadic generic lambda?
问题描述
由于C ++ 14,我们可以使用通用的lambdas:
auto generic_lambda = [](auto param){};
这基本上意味着其调用操作符是基于标记为auto的参数的模板。
问题是如何创建一个lambda,可以接受可变参数,类似于可变函数模板如何工作?如果这是不可能的最接近的事情,可以使用相同的方式是什么?
如何储存?是否可以在 std :: function
?
/ h2>
如何创建可变参数lambda?
泛型lambda使用以下语法:
auto variadic_generic_lambda = [](auto ... param){};
基本上你只需要添加 ...
auto
(可能是ref限定的)和你的参数包名。
p>
auto variadic_generic_lambda = [](auto&& ... param){};
存储
您可以使用 auto
将一个lambda存储在一个可变的泛型lambda中
<变量,或者您可以将其存储在
std :: function
,但您只能使用给予 std :: function
的固定签名来调用它: auto variadic_generic_lambda = [](auto ... param){};
std :: function< void(int,int)> func = variadic_generic_lambda;
func(42,42); //编译
func(lol); //不编译
关于可变常规lambda的集合?
由于每个lambda都有不同的类型,因此您不能将它们的直接类型存储在STL的通常的同类容器中。使用非通用lambdas的方法是将它们存储在相应的 std :: function
中,这将具有固定的签名调用,并且不会限制任何事情,因为您的lambda不是通用的,只能以这种方式调用:
auto non_generic_lambda_1 = [](int,char){ };
auto non_generic_lambda_2 = [](int,char){};
std :: vector< std :: function< void(int,char)>> vec;
vec.push_back(non_generic_lambda_1);
vec.push_back(non_generic_lambda_2);
如存储部分的第一部分所述,如果您可以限制
如果你不能,你将需要某种形式的异构容器,如:
p>-
std :: vector< boost :: variant>
-
std :: vector< boost :: any>
-
boost :: fusion :: >这个问题为异质容器的例子。
还有什么?
关于lambdas的一般信息和关于成员生成的细节参见:
Since C++14 we can use generic lambdas:
auto generic_lambda = [] (auto param) {};
This basically means that its call operator is templated based on the parameters marked as auto.
The question is how to create a lambda that can accept a variadic number of parameters similarly to how a variadic function template would work ? If this is not possible what is the closest thing that could be used the same way ? How would you store it ? Is it possible in a
std::function
?解决方案Syntax
How do you create a variadic generic lambda ?
You can create a variadic generic lambda with the following syntax:
auto variadic_generic_lambda = [] (auto... param) {};
Basically you just add
...
betweenauto
(possibly ref qualified) and your parameter pack name.So typically using universal references would give:
auto variadic_generic_lambda = [] (auto&&... param) {};
Storage
How do you store a variadic generic lambda ?
You can either use
auto
to store a lambda in a variable of its own type, or you can store it in astd::function
but you will only be able to call it with the fixed signature you gave to thatstd::function
:auto variadic_generic_lambda = [] (auto... param) {}; std::function<void(int, int)> func = variadic_generic_lambda; func(42, 42); // Compiles func("lol"); // Doesn't compile
What about collections of variadic generic lambdas ?
Since every lambda has a different type you cannot store their direct type in the usual homogeneous containers of the STL. The way it is done with non generic lambdas is to store them in a corresponding
std::function
which will have a fixed signature call and that won't restrain anything since your lambda is not generic in the first place and can only be invoked that way:auto non_generic_lambda_1 = [] (int, char) {}; auto non_generic_lambda_2 = [] (int, char) {}; std::vector<std::function<void(int, char)>> vec; vec.push_back(non_generic_lambda_1); vec.push_back(non_generic_lambda_2);
As explained in the first part of this storage section if you can restrain yourself to a given fixed call signature then you can do the same with variadic generic lambdas.
If you can't you will need some form of heterogenous container like:
std::vector<boost::variant>
std::vector<boost::any>
boost::fusion::vector
See this question for an example of heterogenous container.
What else ?
For more general informations on lambdas and for details on the members generated see:
这篇关于如何创建一个可变的通用lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!