Can somebody explain why are those two specializations indistinguishable to the compiler (gcc 4.5.1 @ ideone)
template <typename... T> struct S;
template<typename A, typename B, typename... C>
struct S<A, B, C...> {
int f() {return 1;}
};
template<typename... A, typename... C>
struct S< S<A...>, C...> {
int f() {return 2;}
};
and when I try to instantiate S<S<a, b>, a, b> o2; compiler complains:
prog.cpp:20:21: error: ambiguous class template instantiation for 'struct S<S<a, b>, a, b>'
prog.cpp:6:22: error: candidates are: struct S<A, B, C ...>
prog.cpp:11:33: error: struct S<S<A ...>, C ...>
prog.cpp:20:21: error: aggregate 'S<S<a, b>, a, b> o2' has incomplete type and cannot be defined
And when the last specialization is changed to:
template<typename... A, typename B, typename... C>
struct S< S<A...>, B, C...> {
int f() {return 2;}
}
everything works fine.
My understanding of the issue:
Here
S<a,b>matches the second specialization better. However,c, dis a better match for the remaining arguments of the first specialization (single arg + list vs list). Hence it is 1:1.If you comment in
Bin the second specialization, then the second specialization matches better because it is more specialized for the first argument (S<...>) and the rest are equally good.