【发布时间】:2019-11-09 22:42:34
【问题描述】:
我有一组函数,每个函数都采用 T 类型的 N 参数,第二个是 X 类型的附加参数。
对于N=2、T=int 和X=std::vector<int>,双份将是这样的:
void my_function2(int x1, int x2) {}
void my_function2(int x1, int x2, std::vector<int> other) {}
我正在尝试编写一个模板化的高阶函数,我将第一个函数传递给它。
这适用于N=2:
template <typename R, typename T>
int my_hof(R(*param)(T, T)) { param(1,2); }
my_hof(&my_function2); // compiles
明确param 的签名很重要,否则模板实例化会因为“未解决的重载函数类型”而失败。重点是消除my_function2()的第二次重载。
我的问题是我想不出一种方法将其推广到N。我尝试使用可变参数模板:
template <typename R, typename ...X>
struct make_signature { using type = R(*)(int, int); };
// Cheating a big on make_signature not to clutter the question
// But the idea would be to repeat the same type N times.
template <typename R, typename ...X>
int my_generalized_hof(typename make_signature<R, X...>::type param) {
f(1,2); // cheating a bit on the call too
}
my_generalized_hof(&my_function2); // does not compile
我猜编译器会因为param 的类型没有直接给出而感到困惑,它无法决定使用哪个重载来进行类型计算。 (“再次未解决的重载函数类型”)。我认为它也会拒绝进行类型计算,因为它无法推断出R 的类型。
我想不出生成签名的方法。
可以用 C++ 编写吗?
(请注意,我知道我可以通过在调用 my_generalized_hof() 时将 my_function2() 转换为正确的类型来选择重载,但我想避免这种情况)
【问题讨论】:
-
如果我们事先不知道
N是什么,理论上我们必须通过无限数量的“Xs”才能找到正确的重载。如果f过载,则无法从f推断出N。 -
同意!我认为我正在寻找的是某种黑客攻击。就像某种“懒惰”地生成具有正确数量参数的
my_generalized_hof的方法。或者在模板参数推导之前某种方式去激活函数。理想情况下没有宏:)
标签: c++ variadic-templates template-meta-programming template-argument-deduction