可变模板候选者不匹配



我正试图组合一个通用方法调用程序(用于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>());
}

如果没有一个小的用例可编译示例(使用伪类型等),那么使其与代码更紧密地匹配会有点耗时。

最新更新