【发布时间】:2011-12-26 15:45:41
【问题描述】:
我不明白为什么 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的示例
【问题讨论】:
-
原因如下示例:编译器不允许声明
X<int, int*, 10>f,因为它可以匹配模板结构X<T, T*, I>、模板结构X<int, T*, 10>或模板结构X<T, U*, I>,并且这些声明中没有一个比其他声明更匹配。
标签: c++ templates template-specialization specialization partial-specialization