有人可以解释为什么这些两个专业没有区别的编译器(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;}
};
,当我尝试实例S<S<a, b>, a, b> o2;
编译器抱怨:
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
而当最后一个专业化改为:
template<typename... A, typename B, typename... C>
struct S< S<A...>, B, C...> {
int f() {return 2;}
}
一切工作正常。
我想你也可以在这篇文章中添加代码,因为它对于这个问题非常重要。 – UncleBens
@UncleBens代码已添加。 – Predrag