本文介绍了部分模板专业化歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白为什么main中的语句不明确.
I cant see why the statement in main is ambiguous.
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;
}
Isn't X<int, T*, 10>
the most specialized template?This is an example from http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fpartial_specialization.htm
推荐答案
如果每个与第一个匹配的参数列表也与第二个匹配,而与之相反,则模板专用化比另一个专用化.
A template specialization is more specialized than another if every argument list that matches the first also matches the second, but not the other way around.
查看X<int, T*, 10>
和X<T, T*, I>
时:
-
X<int, float*, 10>
与第一个匹配,但与第二个不匹配. -
X<float, float*, 10>
与第二个匹配,但与第一个不匹配.
X<int, float*, 10>
matches the first but not the second.X<float, float*, 10>
matches the second but not the first.
因此,任何一个都不比另一个更专业,并且匹配这两个专业的模板实例化也不会编译.
Therefore neither is more specialized than the other, and a template instantiation that matches both specializations won't compile.
这篇关于部分模板专业化歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!