在C++中,最专业的班级到底是什么意思?
假设我们有以下内容:
template<typename T1, typename T2>
class A {}
template<typename T1, typename T2>
class A<T1*, T2*> {}
template<typename T>
class A<T, T> {}
现在,我知道我们需要选择最专业的班级,但是对于A&lt; double*,double*&gt;,专业人士都有歧义错误,我的老师告诉他们他们有相同的专业水平。但是乍一看,我会说a&lt; t,t&gt;更专业。我们如何得出这个结论? 或者我们怎么能说2种不同的专业水平?
Let's say we have the following:
template<typename T1, typename T2>
class A {}
template<typename T1, typename T2>
class A<T1*, T2*> {}
template<typename T>
class A<T, T> {}
Now, I know that we need to select the most specialized class, but for A<double*, double*>, there is a ambiguity error for both specializations and my teacher told that they are having the same specialization level. But at the first look, I would have said A<T, T> is more specialized. How exactly did we come to this conclusion?
Or how can we say if 2 different specializations are at same level or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,关于术语:这些定义中的每一个都不是 class 的定义。第一个定义定义了A 主要类模板。其他定义定义了该主要模板的部分专业。
不可能按专业化级别对部分专业进行分类。实际规则相当复杂,可以找到参考,例如在这里 。
但是粗略地,如果后者接受所有模板参数列表,那么前者会接受的,但不能相反,您可以考虑比另一个专业化更专业化的部分专业化。
在您的情况下,第一个专业化接受
a&lt; int*,long*&gt;
,但是第二个专业不接受,而第二个则不接受a&lt; int,int,int&gt;
接受。 ,但第一个不是。因此,非正式的情况下,我们可以看到,这两个em>都不是另一个更专业的。主要模板也不比部分专业化更专业,因此
a&lt; double*,double*&gt;
将被所有三个都不是其中的三个接受,而它们是更专业的比所有其他人都模棱两可,并导致该程序的形成不佳。First, regarding terminology: Each of these definitions are not definitions for classes. The first definition defines a primary class template. The other definitions define partial specializations of that primary template.
It is not possible to categorize partial specializations by "levels" of specialization. The actual rules are rather complicated and a reference can be found e.g. here.
But roughly, you can consider a partial specialization more specialized than another, if the latter would accept all template argument lists that the former would accept, but not the other way around.
In your case the first specialization accepts
A<int*, long*>
, but the second one doesn't, and the second one acceptsA<int, int>
, but the first one doesn't. So informally, we can see that neither is more specialized than the other.The primary template is not more specialized than the partial specializations either, so
A<double*, double*>
which would be accepted by all three without one of them being more specialized than all the others, is ambiguous and causes the program to be ill-formed.