模板类成员函数仅特化
我正在阅读《模板完整指南》,其中内容如下:
它正在谈论类模板专业化。
虽然可以专门化一个成员函数 类模板,一旦你这样做了,你就不能再专门化 专门成员所属的整个类模板实例 到。
我实际上想知道这是怎么回事,因为你可以在没有任何成员函数的情况下专门化。是不是说不能进行仅包含一个成员函数的专业化,然后再进行另一个包含所有成员函数的专业化?
有人可以澄清一下吗?
I am reading the Complete Guide on Templates and it says the following:
Where it is talking about class template specialization.
Although it is possible to specialize a single member function of a
class template, once you have done so, you can no longer specialize
the whole class template instance that the specialized member belongs
to.
I'm actually wondering how this is true, cause you can specialize without any member functions at all. Is it saying that you cannot have a specialization with only one member function and then another with all member functions?
Can someone please clarify?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为它指的是以下情况:
一旦完成,您就无法将完整模板专门化为任何其他东西,所以
I think it is referring to the following case:
Once that is done, you cannot specialize the full template to be any other thing, so
我认为这意味着你可以:
专门化整个类和所有成员(数据和函数,静态与否,虚拟与否)都必须声明和定义,即使它们与非相同。专门化版本,
专门化一些函数成员,但是你不能专门化整个类(即所有成员的声明方式与非特化情况相同,您只需提供某些函数成员的实现)。
I think what is meant is that you can either:
specialize the whole class and all members (data and functions, static or not, virtual or not) have to be declared and defined even if they are the same as for the non specialized version,
specialize some function members, but then you can't specialize the whole class (i.e. all members are declared in the same way as for the non specialized case, you just provide the implementation for some function members).
可以记住显式和隐式实例化以及类似以下的情况。
当一个类被显式实例化时(
template class Class_Template;
),泛型类的所有成员都将出现在实例化的类中。然而,假设通过提及类主体来描述类专业化。只有在专用类中显式定义的成员(无论是通用类中已经存在的成员还是新成员)才会出现在实例化类中。假设(泛型类的)成员函数是通过特定范围的类特化来定义的,那么隐式实例化一个特化类,该类将以适当的特化方式具有这些函数以及泛型类中存在的所有其他成员。
在第二种情况下,由于专门化的类已被隐式实例化,因此我们以后无法通过显式实例化或通过提及类主体来定义具有相同专门化的类。为了添加特定于该专业的成员,可能需要执行后者。在这种情况下这是不可能的,因为编译器已经实例化了一个专门的类。
无论实例化类专业化的这些方式如何,用户始终可以自由地基于其他模板参数实例化其他专业化。
Explicit and implicit instantiation and situations like the following could be kept in mind.
When a class is explicitly instantiated (
template class Class_Template<Explicit_Type>;
), all members of the generic class will be present in the instantiated class. However, suppose a class specialisation is described by mentioning a class body. Only members (whether members that are already present in the generic class or new members) that are explicitly defined in the specialized class will be present in the instantiated class.Suppose member functions (of the generic class) are defined via a particular scoped class specialization, then a specialized class is implicitly instantiated which will have these functions and also all other members present in the generic class, in a suitably specialized manner.
In the second case, since a specialized class has been implicitly instantiated, we cannot later also define a class with the same specialization, either via explicit instantiation or by mentioning a class body. The need to do the latter may arise in order to add a member that is specific to that specialization. That is not possible in this case, since the compiler has already instantiated a specialized class.
Irrespective of these ways in which a class specialization could be instantiated, the user is always free to instantiate other specializations based on other template arguments.