模板特化是扩展还是覆盖通用模板? [英] Does a template specialization extend or override the generic template?

查看:30
本文介绍了模板特化是扩展还是覆盖通用模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

template<typename T>
struct A{
    void method1(){}
 };

template<>
struct A<int>{
    void method2(){}
 };

A 会同时拥有 method1 和 method2 吗?而 A 只会有 method1 吗?

Will A<int> have both method1 and method2? And A<float> will only have method1 ?

推荐答案

每个特化都会带来一个全新的数据类型(或者一个全新的模板,如果特化只是部分的).来自标准 (C++11):

Each specialization brings an entirely new data type into existence (or an entirely new template, if the specialization is only partial). From the Standard (C++11):

(§14.5.5/2) 每个类模板部分特化是一个不同的模板,并且应该为模板部分特化 (14.5.5.3) 的成员提供定义.

(§14.5.5/2) Each class template partial specialization is a distinct template and definitions shall be provided for the members of a template partial specialization (14.5.5.3).

还有:

(§14.5.5.3/1) [...] 类模板部分特化的成员与主模板的成员无关.应定义以需要定义的方式使用的类模板部分特化成员;主模板成员的定义永远不会用作类模板部分特化成员的定义.[...]

(§14.5.5.3/1) [...] The members of the class template partial specialization are unrelated to the members of the primary template. Class template partial specialization members that are used in a way that requires a definition shall be defined; the definitions of members of the primary template are never used as definitions for members of a class template partial specialization. [...]

以上内容是在部分专业化的上下文中陈述的,但它也适用于显式专业化(如您的情况),尽管标准没有明确说明这一点.

The above is stated in the context of partial specializations, but it applies to explicit specializations (as in your case) as well, although the Standard does not say this very clearly.

另请注意,您不仅需要在特化中声明您想要的所有成员函数,还需要定义它们(这里,即使对于显式特化,标准也非常清楚):

Also note that you need not only declare all member functions that you want in a specialization, but you need to define them, too (here, the Standard is very clear even about explicit specializations):

(14.7.3/5) 显式特化类的成员不会从类模板的成员声明中隐式实例化;相反,如果需要定义类模板特化的成员,则应明确定义其本身.在这种情况下,类模板显式特化的定义应在范围内在定义成员的点.显式特化类的定义与生成特化的定义无关.也就是说,它的成员不需要与生成的专业化的成员具有相同的名称、类型等.[...]

(14.7.3/5) A member of an explicitly specialized class is not implicitly instantiated from the member declaration of the class template; instead, the member of the class template specialization shall itself be explicitly defined if its definition is required. In this case, the definition of the class template explicit specialization shall be in scope at the point at which the member is defined. The definition of an explicitly specialized class is unrelated to the definition of a generated specialization. That is, its members need not have the same names, types, etc. as the members of a generated specialization. [...]

所以,确实,A 只会有 method2(),而 A 只会有 method1() 作为成员.此外,如果您还要在 A<int> 特化中引入 method1(),它不需要与 A< 具有相同的参数类型或返回类型;float>::method1().

So, indeed, A<int> will only have method2(), and A<float> will only have method1() as member. Furthermore, if you were to introduce method1() in the A<int> specialization as well, it needs not have the same argument types or return type as A<float>::method1().

请参阅@aschepler 的回答,了解避免为 int 情况重写模板定义的可能方法.

See @aschepler's answer for possible ways to avoid having to rewrite the template definition for the int case.

这篇关于模板特化是扩展还是覆盖通用模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆