Reputation: 66200
In order to practice with C++11, I'm playing with variadic templates.
In particular, I'm playing with a sort of recursive variadic container class (onion
) and a function that returns the number of template types (func()
).
I came across a case that the clang++ (3.5.0) can't compile while the g++ (4.9.2) compiles and runs with no problems.
I have simplified it as follows
#include <iostream>
template <typename F, typename ... O>
struct onion;
template <typename F, typename S, typename ... O>
struct onion<F, S, O...>
{
F first;
onion<S, O...> others;
};
template <typename L>
struct onion<L>
{ L last; };
template <typename ... Args>
std::size_t func (onion<Args...> const &)
{ return sizeof...(Args); }
int main()
{
auto o = onion<int, char const *, float> { 23, { "abcd", {34.0f}} };
std::cout << func(o) << '\n';
return 0;
}
clang++ (3.5.0) gives me the following compiler error
test.cpp:28:17: error: no matching function for call to 'func'
std::cout << func(o) << '\n';
^~~~
test.cpp:20:13: note: candidate template ignored: substitution
failure [with Args = <>]: too few template arguments for class template
'onion'
std::size_t func (onion<Args...> const &)
^ ~~~~~
1 error generated.
g++ (4.9.2) compiles without problem and, running, output 3
.
My question is: who's right?
clang++, giving an error, or g++, compiling?
I add that if I rewrite func()
in this way
template <typename X, typename ... Args>
std::size_t func (onion<X, Args...> const &)
{ return 1U + sizeof...(Args); }
or if I redefine onion
in this way
template <typename ... O>
struct onion;
both clang++ and g++ compile without errors.
Upvotes: 1
Views: 66
Reputation: 11317
This looks like a compiler bug in clang 3.5. If I run the code with the trunk version it compiles nicely.
Upvotes: 1