可变参数的冲突类型
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)不会像我期望的那样使用不同的参数集使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>{}
作为参数传递,你传递一个对象,但在它里面,你有一组可用的方法。
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- C++哈希表 - 如何解决自定义数据类型作为键的unordered_map冲突?
- 模板冲突的类型-但类型应该是相同的cfr类层次结构
- 冲突声明/重新定义:不同的基本类型
- 可变参数的冲突类型
- 致命错误LNK1112:在 npm 安装期间,模块计算机类型'X86'与目标计算机类型'x64'冲突
- GCC 6.3.0 中的 ODR 冲突,类型在两个单独的翻译单元中定义
- 通过参考推断模板包中的冲突类型
- 错误:在 mingw64 上构建 cmake+boost 项目时'GetSystemTimeAsFileTime'的类型冲突
- 致命错误LNK1112:通过 vcvarsall .bat x86 运行构建'X86'模块计算机类型'x64'与目标计算机类型冲突
- 冲突的声明类型/价值不匹配
- 如何解决"sprintf"的冲突类型?
- 忽略候选模板:推断参数的冲突类型:<常量 T &> 与 <T &>
- 错误:调用"反向"没有匹配函数:候选模板被忽略:推断出参数的冲突类型
- 为虚拟析构函数指定的冲突类型属性
- C++ 模板错误,并推断参数的冲突类型
- 推导参数'T'的冲突类型以供通用参考
- C++错误:推断出参数'T'字符串与常量字符的冲突类型 *
- 函数和冲突类型的隐式声明- AVR
- 'memchr'的冲突类型