问题描述
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;
}
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
满足某个条件,如果满足,则调用函子。像下面的伪代码:
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
并将它们传递给 {{ 1}}?
解决方法
在 C++17 中,这很容易使用 fold expressions 解决。
#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"
为真时,才会计算运算符的右侧。
稍微不那么hacky的方法使用单独的函数:
template <typename Functor>
void InvokeIfNamedF(const Functor &functor) {
if (functor.GetName() == "f")
functor();
}
template <typename... Functors>
void ForEachFunctor(const Functors&... functors)
{
(InvokeIfNamedF(functors),...);
}
参数包的元素使用逗号运算符组合。然而,在这个例子中,我们不带参数调用每个函子。如果所有函子都有不同的签名,那么将它们作为一个包传递并一次有条件地调用它们可能是不可能的。