[英]Conflicting types for variadic parameter
我试图编写一个通用的调用函数。
它具有以下语法:
template<int Index, typename ReturnType, typename... Parameter>
ReturnType invokeGlobalFunction(Parameter... parameters)
{
return invocator->invoke<ReturnType>(Index, parameters...);
}
接下来,我尝试从中派生两个不同的功能点,如下所示:
registerFunction(::someGlobalFunction, &invokeGlobalFunction<0, void>);
registerFunction(::someOtherFunction, &invokeGlobalFunction<1, int>);
其中someGlobalFunction
的原型为void someGlobalFunction()
, someOtherFunction
的原型为int someOtherFunction(int, const char *)
。
在第一个调用中,它像一个超级按钮一样工作,但是第二个调用抛出错误: candidate template ignored: deduced conflicting types for parameter 'Parameter' (<int, const char *> vs. <>)
。
这意味着,编译器(在Ubuntu系统上为g ++ 7.4.0 btw。)不会像我期望的invokeGlobalFunction
用不同的参数集重载invokeGlobalFunction
。
注意:当我在调用中显式设置参数类型时
registerFunction(::someOtherFunction, &invokeGlobalFunction<1, int, int, const char *>);
编译器很高兴地采用了它,但是如果可能的话,我想避免这种情况。
另外,如果每次索引更改时我都能以某种方式创建一个唯一的函数,那将很棒,因为这将使我拥有具有相同参数但返回类型不同的函数(据我所知,这是非法的)。
谢谢。
但我想避免这种情况。
据我所知,还没有模板功能。
问题在于模板参数不是单个对象而是一组对象,其中函数只能从该对象接受一个对象。
当你写
&invokeGlobalFunction<1, int>
您选择Index = 1
, ReturnType = int
和(这就是重点)一个空的Parameter...
列表的精确函数。
建议:如果可以,请使用模板方法在模板struct
转换invokeGlobalFunction()
。
像
template <int Index, typename ReturnType>
struct invokeStruct
{
template <typename ... Parameters>
ReturnType operator() (Parameters ... parameters)
{
// ...
}
};
这样,您就有了一个结构体,每个结构体中都有一组operator()
; 通过传递invokeStruct<1, int>{}
作为参数,您传递了一个对象,但是在它内部,您可以使用一组方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.