问题
I have a function foo
that takes a variadic function pointer as its argument.
I would like to use "using" to define the argument's type prior to the function declaration.
template <typename ... vARGS>
using TFuncType = void(*)(vARGS ... V_args);
template <typename ... vARGS>
void foo(TFuncType<vARGS ...> funcptr) {}
void bar(int i) {}
int main() {
foo(&bar); // This line fails to compile.
}
This doesn't compile. The error (via clang using c++1z) is:
/make/proj/test/variadic-funcparam-deduce2.cpp:39:5: error: no matching function for call to 'foo'
foo(&bar);
^~~
/make/proj/test/variadic-funcparam-deduce2.cpp:33:36: note: candidate template ignored: substitution failure [with vARGS = int]
template <typename ... vARGS> void foo(TFuncType<vARGS ...> funcptr) {}
Why is the "int" substitution failing?
I can successfully compile if I explicitly write the type inside foo():
template <typename ... vARGS>
void foo(void(*funcptr)(vARGS ... V_args)) {}
But I cannot get the initial ("using") version to work even when explicitly specifying the template parameters, and using a pre-casted TFuncType<int>
for the argument, i.e.:
int main() {
TF_call<int> fptr = &bar; // This line is OK.
foo<int>(fptr);
}
Does anyone know what's up here?
Is there something strange about using typedef'd ("using") variadics and/or function pointers that I'm missing?
回答1:
I believe this may be related to the following text which I copied from this answer that itself takes from the C++ standard in 14.5.7 [temp.alias] paragraph 2:
When a template-id refers to the specialization of an alias template, it is equivalent to the associated type obtained by substitution of its template-arguments for the template-parameters in the type-id of the alias template. [ Note: An alias template name is never deduced. — end note ]
If I'm interpreting that right, it means that GCC accepting the code is actually non-conforming.
来源:https://stackoverflow.com/questions/31225888/pre-typedefing-a-variadic-function-pointer-argument