可变模板候选者不匹配
Variadic template candidate unmatched
我正试图组合一个通用方法调用程序(用于C++OO/v8桥),使用可变模板元编程来构建参数列表,转换为本机类型,并在传入参数列表为空(因此生成传出)后最终执行所附方法:
template<typename... PARAMS>
class InvocationBuilder {
public:
void invoke(const Arguments &source, PARAMS&... params) {
cout << "Invoke" << endl;
(instance->*(method))(*params...);
}
template<class HEAD, class ... TAIL>
void invoke(const Arguments &source, PARAMS... params) {
cout << "Expand" << endl;
Type<HEAD> param(source[sizeof...(PARAMS)]);
InvocationBuilder<PARAMS..., HEAD> builder;
builder.template invoke<TAIL...>(source, params..., *param);
}
Type类只是一个包装器,用于创建v8参数的堆栈范围变体(例如,在调用期间,char*字符串可以在范围内使用,但在调用返回后会自动清除)。
现在,当实际的网桥使用参数列表调用它时,使用:
InvocationBuilder<> builder;
builder.template invoke<ARGS...>(args);
其中args是v8::Arguments引用。
编译器正确地链接了参数生成的每个步骤,但未能匹配非模板化的invoke()方法,从而实际执行本机C++方法。
错误消息如下:
include/link/function.hh: In member function 'void sjs::link::InstanceFunctionVariadic<CLASS, ARGS>::InvocationBuilder<PARAMS>::invoke(const v8::Arguments&, PARAMS ...) [with HEAD = int, TAIL = {}, PARAMS = {int, int}, CLASS = SomeClass, ARGS = {int, int, int}]':
include/link/function.hh:65:6: recursively instantiated from 'void sjs::link::InstanceFunctionVariadic<CLASS, ARGS>::InvocationBuilder<PARAMS>::invoke(const v8::Arguments&, PARAMS ...) [with HEAD = int, TAIL = {int}, PARAMS = {int}, CLASS = SomeClass, ARGS = {int, int, int}]'
include/link/function.hh:65:6: instantiated from 'void sjs::link::InstanceFunctionVariadic<CLASS, ARGS>::InvocationBuilder<PARAMS>::invoke(const v8::Arguments&, PARAMS ...) [with HEAD = int, TAIL = {int, int}, PARAMS = {}, CLASS = SomeClass, ARGS = {int, int, int}]'
include/link/function.hh:47:5: instantiated from 'v8::Handle<v8::Value> sjs::link::InstanceFunctionVariadic<CLASS, ARGS>::run(const v8::Arguments&) [with CLASS = SomeClass, ARGS = {int, int, int}]'
test.cc:41:1: instantiated from here
include/link/function.hh:65:6: error: no matching function for call to 'sjs::link::InstanceFunctionVariadic<SomeClass, int, int, int>::InvocationBuilder<int, int, int>::invoke(const v8::Arguments&, int&, int&, int)'
include/link/function.hh:65:6: note: candidate is:
include/link/function.hh:61:10: note: template<class HEAD, class ... TAIL> void sjs::link::InstanceFunctionVariadic<CLASS, ARGS>::InvocationBuilder::invoke(const v8::Arguments&, PARAMS ...) [with HEAD = HEAD, TAIL = {TAIL ...}, PARAMS = {int, int, int}, CLASS = SomeClass, ARGS = {int, int, int}]
该消息清楚地表明,对于C++实例方法void test(int a,int b,int C),前三个步骤工作正常,使用Type提取参数并传递结果,但我不知道为什么没有正确使用最终invoke()。
我已经尝试过将其完全专门化,但后来我收到了关于命名空间范围之外的专门化的错误消息(我认为这是因为该方法是模板化类的成员)。
我还尝试过移动传入/传出参数列表,以便传入在类variadic模板中,而传出在方法中专门用于调用类,但我遇到了关于将variadic解包到静态模板中的"抱歉,未实现"消息。
我也尝试过使用一个通用的单变量模板来解决这个问题,然后专门针对HEAD/TAIL情况,并专门针对空集情况,但随后我会立即出现歧义(可能是因为HEAD/TAIL值实际上并不是作为参数传递的,只是在模板中)。
但到目前为止,还没有骰子。有人有其他想法,或者可以解释我哪里错了吗?
注意:
- 显式地尝试调用模板
- 模板化的
invoke
总是要求将PARAMS...
作为函数参数传递
一种可能的替代方案:
#include <functional>
template<class... Types> struct List {};
template<class... PARAMS> struct Invoker {
typedef std::function<void (PARAMS&...)> Fn;
Fn fn_;
Invoker(const Fn&& fn) : fn_(fn) {}
void invoke(List<>, PARAMS&... params) {
fn_(params...);
}
template<class HEAD, class... TAIL, class... ARGS>
void invoke(List<HEAD, TAIL...>, ARGS... params) {
HEAD param; // or your Type<HEAD> ... etc.
invoke(List<TAIL...>(), params..., param);
}
};
void f(int, int, int) {} // some function you want to call
int main() {
Invoker<int,int,int> inv(&f);
inv.invoke(List<int,int,int>());
}
如果没有一个小的用例可编译示例(使用伪类型等),那么使其与代码更紧密地匹配会有点耗时。
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 可变模板候选者不匹配