Variadic template candidate unmatched
I'm attempting to put together a generic method invoker (for a C++ OO/v8 bridge), using variadic template metaprogramming to build the parameter list, converting to native types, and finally execute the attached method, once the incoming param list is empty (and outgoing is therefore built):
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);
}
The Type class is merely a wrapper to create stack scoped variants of v8 parameters (so that, for instance, char* strings can be used while in scope during the invocation, but are automatically cleaned up once the call has returned).
Now, when the actual bridge invokes this, with a parameter list, using:
InvocationBuilder<> builder;
builder.template invoke<ARGS...>(args);
Where args is the v8::Arguments reference.
The compiler correctly chains each step of the parameter generation, but fails to match the non-templated invoke() method, to actually execute the native C++ method.
The error message is as follows:
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}]
The message clearly shows that the first three steps, for a C++ instance method void test(int a, int b, int c), are working correctly, extracting the parameters using Type and passing the results along - but I can't work out why the final invoke() is not being correctly used.
I've tried to fully specialize it, but then I get error messages about specialization outside of namespace scope (which I assume is because the method is a member of a templated class).
I've also tried shifting the incoming/outgoing parameter lists, so that the incoming is in the class variadic template, and the outgoing in the method to specialise the class for invocation instead - but I run into the "sorry, unimplemented" message about unpacking the variadic into the static template.
I've also tried working around that by using a generic single variadic template, then specialising for the HEAD/TAIL case, and specialising for the empty set case, but then I get immediate ambiguity (probably because the HEAD/TAIL values are not actually passed as parameters - merely in the template).
But so far, no dice. Anyone have any other ideas, or can explain where I'm going wrong?
Note that:
invoke
always requires PARAMS...
to be passed as function arguments One possible alternative:
#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>());
}
... without a small compilable example of your use-case (with dummy-types etc.) it would be a bit time-consuming to make this match your code more closely.
链接地址: http://www.djcxy.com/p/66890.html上一篇: 模板模板variadics在C ++中
下一篇: Variadic模板候选人无与伦比