[英]Call a variadic template functor in a variadic template parameter pack if it satisfies a condition
我有一个可变参数模板函子 class:
template <typename Result, typename... Arguments>
class Functor
{
public:
using FunctionType = std::function<Result(Arguments...)>;
Result operator() (const Arguments&... arguments) { return Function(arguments); }
std::string GetName() { return Name; }
Functor(const std::string& name, const FunctionType& function) : Name(name), Function(function) { }
private:
std::string Name;
FunctionType Function;
}
和一个可变参数模板 function:
template <typename... Functors>
void Function(const Functors&... functors) { }
假设我已经声明了一些Functor
并想在Function
中使用它们:
Functor<int, int> f
(
"f",
[](int x) -> int { return 2 * x - 1; }
);
Functor<int, int, int> g
(
"g",
[](int x, int y) -> int { return x + 2 * y - 3; }
);
Function(f, g);
在Function
内部,我想找出参数包中传递给它的哪个Functor
满足某个条件,如果满足,则调用该 functor。 像下面的伪代码:
template <typename... Functors>
void Function(const Functors&... functors)
{
foreach (Functor functor in functors)
{
if (functor.GetName() == "f")
{
functor(); // the functor can have different parameter lists so this is another problem
}
}
}
我想知道是否有办法做到这一点。 我还想知道,由于Functor
可以有不同的参数列表,即使我能够找到正确的Functor
来调用它们,我该如何调用它们? 假设有一个std::vector<int>
并且当Functor
采用三个int
作为其参数时,是否可以采用前三个int
并将它们传递给Functor
?
在 C++17 中,这很容易使用折叠表达式解决。
#include <functional>
#include <string>
template <typename Result, typename... Arguments>
class Functor
{
public:
using FunctionType = std::function<Result(Arguments...)>;
Result operator() (const Arguments&... args) {
return this->Function(arguments...);
}
std::string GetName() {
return this->Name;
}
Functor(const std::string& name, const FunctionType& function)
: Name(name), Function(function) { }
private:
std::string Name;
FunctionType Function;
};
template <typename... Functors>
void ForEachFunctor(const Functors&... functors)
{
((functors.getName() == "f" && functors()), ...);
}
在这里,我们利用&&
运算符的短路。 只有当条件functors.getName() == "f"
为真时,才会计算运算符的右侧。
稍微不那么老套的方法使用单独的 function:
template <typename Functor>
void InvokeIfNamedF(const Functor &functor) {
if (functor.GetName() == "f")
functor();
}
template <typename... Functors>
void ForEachFunctor(const Functors&... functors)
{
(InvokeIfNamedF(functors), ...);
}
参数包的元素使用逗号运算符组合。 然而,在这个例子中,我们调用了每个没有参数的函子。 如果所有仿函数都有不同的签名,那么将它们作为一个包传递并一次有条件地调用它们可能是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.