C ++作为标准禁止在单个类实例中存储成员函数吗? [英] Does C++ as a standard prohibit the storage of member functions inside individual class instances?

查看:80
本文介绍了C ++作为标准禁止在单个类实例中存储成员函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++实现中,通常代码不会以类实例的形式存储(以任何形式)。代码段不在与对象等相同的存储器空间中。这意味着成员函数不会在类实例内存储。

In C++ implementations, typically code is not stored (in any form) inside class instances. The code segment is not in the same memory space as objects and the like. This means that member functions are not "stored" inside class instances.

但是当问了一个关于这个的问题,我想知道:在什么程度上,如果有的话,标准禁止成员函数存储在它们的封装类中,实例化类创建这些函数的副本?理论上,我能做一个实现这样工作吗?

But when a question was asked about this, I got to wondering: to what extent, if at all, does the standard prohibit member functions being stored inside their encapsulating class, to the extent that instantiating the class makes a copy of those functions? Theoretically, could I make an implementation that worked this way? And could it even remotely abide by the common ABIs?

推荐答案

如果在C ++中,代码是一个一级的值,那么它可以远程地遵守常见的ABI成员函数的代码将只是一个 const static 类成员,并且你不会再希望在一个实例中找到的,而不是任何其他静态数据成员。 (&; 9.4.2:静态数据成员不是类的子对象的一部分)

If, in C++, code were a first-class value, then the code for a member function would be simply a const static class member, and you would no more expect to find that in an instance than you would any other static data member. (§ 9.4.2: "A static data member is not part of the subobjects of a class.")

但是,代码不被认为是一个值,你甚至不能构造一个指向成员函数的指针(虽然你可以构造一个指向成员的指针,这不是一个真正的指针,因为它是不可用的,没有引用一个实例)。这使得成员函数代码不同于静态数据成员和非成员函数,二者都允许创建独立指针,其还具有(或多或少)排除复制的等式保证。

However, code is not considered a value, and furthermore you cannot even construct a pointer to a member function (although you can construct a "pointer to member", that is not really a pointer since it is not usable without a reference to an instance). That makes member function code different both from static data members and non-member functions, both of which allow the creation of free-standing pointers, which furthermore have equality guarantees which (more or less) preclude copying.

类实例包含对 virtual 成员函数的引用(在大多数实现中间接;指针实际上是静态vtable),当新实例创建。对引用的大小没有要求,所以在理论上(据我所知),没有什么可以阻止实现避免内联并且为类的每个实例重新存储整个代码。

Class instances do contain a reference to virtual member functions (indirectly, in most implementations; the pointer is actually to a static vtable) which must be copied when a new instance is created. No requirement is made on the size of the reference, so in theory (as far as I know) there is nothing to stop an implementation from avoiding the indirections and storing the entire code anew for each instance of the class.

但是标准布局类型有一个例外,它是没有virtual成员函数的类的子集,用§表示; 9.12 / 18,这要求具有相同初始成员的两种标准布局类型具有与初始成员相同的布局。回想一下,标准布局对象必须简单地用memcpy(§ 3.9 / 3)可复制,在内存中必须是连续的(> 1.8 / 5),并且必须按顺序包含它们的成员(&使得有效地不可能在任何标准布局对象中包括类特定的静态数据,这将包括成员函数的代码。

But there is an exception for standard-layout types, which is a subset of classes with no virtual member functions, expressed in § 9.12/18, which requires that two standard-layout types with identical initial members have identical layout for the initial members. Recalling that standard-layout objects must be simply copyable with memcpy (§3.9/3), must be contiguous in memory (§1.8/5), and must include their members in order (§9.12/13), this requirement makes it effectively impossible to include class-specific static data in any standard-layout object, which would include the code for member functions.

因此我得出结论,至少对于标准 - 布局对象,C ++标准禁止在对象表示中存储静态数据,包括成员函数的代码。

So I conclude that at least for standard-layout objects, the C++ standard does prohibit the storage of static data, including code for member functions, within the object representation.

这篇关于C ++作为标准禁止在单个类实例中存储成员函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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