部分模板专业化模糊

时间:2011-12-26 15:45:41

标签: c++ templates template-specialization specialization partial-specialization

我不明白为什么main中的陈述含糊不清。

template<class T, class U, int I> struct X
{ void f() { cout << "Primary template" << endl; } };


template<class T, int I> struct X<T, T*, I>
{void f() { cout << "Partial specialization 1" << endl;}};

template<class T, class U, int I> struct X<T*, U, I>
{void f() { cout << "Partial specialization 2" << endl;}};

template<class T> struct X<int, T*, 10>
{void f() { cout << "Partial specialization 3" << endl;}};

template<class T, class U, int I> struct X<T, U*, I>
{void f() { cout << "Partial specialization 4" << endl;}};

 int main()
 {
   X<int, int*, 10> f;
 }

X<int, T*, 10>不是最专业的模板吗? 这是http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fpartial_specialization.htm

的示例

1 个答案:

答案 0 :(得分:10)

如果与第一个匹配的每个参数列表也与第二个匹配,则模板特化比另一个更专业化,但不是相反。

查看X<int, T*, 10>X<T, T*, I>

  • X<int, float*, 10>匹配第一个但不匹配第二个。
  • X<float, float*, 10>匹配第二个但不匹配第一个。

因此,两者都不比另一个更专业,并且匹配这两个特化的模板实例化将无法编译。

相关问题