Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing function template specializations to a variadic template function

I have no problem passing the address of a function template specialization to a regular template function:

template <typename T>
void f(T) {}

template <typename A, typename B>
void foo(A, B) {}

int main()
{
    foo(&f<int>, &f<float>);
}

However, when I try to pass the same specializations to a variadic template:

template <typename T>
void f(T) {}

template <typename... A>
void bar(A...) {}

int main()
{
    bar(&f<int>, &f<float>);
}

I get the following compiler errors with GCC (I tried 4.6.1 and 4.7.0):

test.cpp: In function 'int main()':
test.cpp:9:27: error: no matching function for call to 'bar(<unresolved overloaded function type>, <unresolved overloaded function type>)'
test.cpp:9:27: note: candidate is:
test.cpp:5:6: note: template<class ... A> void bar(A ...)
test.cpp:5:6: note:   template argument deduction/substitution failed:

Why am I getting these errors?

like image 969
HighCommander4 Avatar asked Sep 12 '11 21:09

HighCommander4


People also ask

What is Variadic template in C++?

Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration.

What is the use of Variadic templates?

With the variadic templates feature, you can define class or function templates that have any number (including zero) of parameters. To achieve this goal, this feature introduces a kind of parameter called parameter pack to represent a list of zero or more parameters for templates.

What is a Variadic function in C?

Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.

What means variadic?

variadic (not comparable) (computing, mathematics, linguistics) Taking a variable number of arguments; especially, taking arbitrarily many arguments.


1 Answers

Looks like it might be a bug in GCC that is possibly fixed in GCC 4.6.2 (I say possibly because it's not exactly the same, but does involve getting the address of a variadic template function).

like image 82
Peter Alexander Avatar answered Sep 28 '22 22:09

Peter Alexander