Can the compiler exploit empty base optimisation if the class contains a member of the base class?

青春壹個敷衍的年華 提交于 2019-12-23 06:57:13

问题


Consider

struct base {};
struct child : base {};

It's well-known that sizeof(child) can be 1 by application of the empty base optimisation.

Now however, consider

struct base {};
struct child : base {base b;};

Can the compiler apply the empty base optimisation now, or must sizeof(child) be at least 2?

Reference: http://en.cppreference.com/w/cpp/language/ebo


回答1:


No, it cannot. From the same reference:

Empty base optimization is prohibited if one of the empty base classes is also the type or the base of the type of the first non-static data member

Thus sizeof(child) >= 2.




回答2:


The rule is that sub-objects of the same type cannot be at the same address. You have 2 X sub-objects here, hence each one must be at a different address.

Objects of the same type cannot share the same address because the identity of an object in C++ is its address. If multiples objects of the same type share the same address they are indistinguishable. And this is why the minimum complete object size is 1, so that each object in an array has a distinct address. See "§ The C++ object model [intro.object]":

An object is a region of storage.

...

Objects can contain other objects, called subobjects. A subobject can be a member subobject (9.2), a base class subobject (Clause 10), or an array element. An object that is not a subobject of any other object is called a complete object.

...

Unless it is a bit-field (9.6), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class subobjects may have zero size.

...

Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two objects that are not bit-fields may have the same address if one is a subobject of the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they shall have distinct addresses.

This is why, for example, boost::noncopyable can increase a class size if one inherits it more than once unwittingly indirectly through empty base classes. E.g. in:

struct A : boost::noncopyable {};
struct B : boost::noncopyable {};
struct C : boost::noncopyable {};
struct D : A, B, C {};

sizeof(D) == 3 because there are three distinct boost::noncopyable sub-subjects. If derivation from boost::noncopyable is dropped, then sizeof(D) == 1.




回答3:


Objects in C++ are required to have unique "identity". From [intro.object]/8 (N4659):

Two objects a and b with overlapping lifetimes that are not bit-fields may have the same address if one is nested within the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they have distinct addresses.

A base class subobject and a member subobject are separate objects; neither is "nested within" the other. Therefore, if they are of the same type, they must have separate addresses.

Note that this extends recursively. Consider the following:

struct eb1 {};

struct eb2 : eb1 {};
struct not_empty(eb1 a;};

struct derived : eb2 {not_empty b;};

Because of the unique identity rule of C++, derived::eb2::eb1 must have a different address from derived::b::a. Therefore, the compiler cannot employ EBO on derived.




回答4:


I'll plop in another more basic quote

[intro.object]

Two objects a and b with overlapping lifetimes that are not bit-fields may have the same address if one is nested within the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they have distinct addresses.


Since b is not an subobject of the inherited base, they must have distinct addresses.



来源:https://stackoverflow.com/questions/44908996/why-does-sizeofd-equal-2-in-this-code-see-details

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!