0

The paragraph below is from the book "C++ Templates. The Complete Guide", an it was copied from this site.

However, even though there are no zero-size types in C++, the C++ standard does specify that when an empty class is used as a base class, no space needs to be allocated for it provided that it does not cause it to be allocated to the same address as another object or subobject of the same type. Let's look at some examples to clarify what this so-called empty base class optimization (or EBCO) means in practice. Consider the following program: ...

Alexander
  • 2,581
  • 11
  • 17
  • References at the bottom of the page: https://en.cppreference.com/w/cpp/language/ebo – Richard Critten Sep 05 '20 at 20:04
  • @RichardCritten _References at the bottom of the page_ Which do not mention the place where it is specified. – Language Lawyer Sep 05 '20 at 20:19
  • For C++11, it is specified in [\[intro.object\]/6](https://timsong-cpp.github.io/cppwp/n3337/intro.object#6). IDK where it is specified in C++03. – Language Lawyer Sep 05 '20 at 20:27
  • @LanguageLawyer [This sentence](https://timsong-cpp.github.io/cppwp/intro.object#8.sentence-2) in \[intro.object\]/8 seems to answer my question. But I still don't understand what do they mean by the [sentence that follows](https://timsong-cpp.github.io/cppwp/intro.object#8.sentence-3) in the paragraph. Do you have anything to say about this? – Alexander Sep 05 '20 at 20:46
  • It just says that the implementation may or may not apply empty-base optimization; that in general, the optimization is allowed but is not required. The first sentence you cite defines one case where this optimization is in fact mandatory. – Igor Tandetnik Sep 05 '20 at 22:33
  • @IgorTandetnik _The first sentence you cite defines one case where this optimization is in fact mandatory_ Of course, this is not correct. [Two objects with overlapping lifetimes that are not bit-fields **may** have the same address if ... at least one is a subobject of zero size ...](https://timsong-cpp.github.io/cppwp/intro.object#9.sentence-2). I.e. the optimization is only **allowed** for objects of so-called zero size, but not required. – Language Lawyer Sep 06 '20 at 00:01
  • _But I still don't understand what do they mean by the sentence that follows in the paragraph._ I don't understand what can cause difficulties with that sentence. It says that an implementation may extend the list of cases when objects are of zero size (as long it doesn't contradict https://timsong-cpp.github.io/cppwp/intro.object#8.sentence-1) – Language Lawyer Sep 06 '20 at 00:04
  • @LanguageLawyer: "*the optimization is only allowed for objects of so-called zero size, but not required*" Except that the common-initial-sequence rules for unions effectively require that, given a `union{ T1 t1[10]; T2 t2[10]; };`, if `T1` and `T2` are standard layout and layout compatible, and `t1` is the active member, you can read the same members through `t2`. And that's only possible to implement if `T1` and `T2` are the *same size* (and layout). Which means that if `T1` has no base class and `T2` has an empty base class, implementations are *required* to give it zero size. – Nicol Bolas Sep 06 '20 at 01:00
  • @LanguageLawyer: In short: EBO is required for standard layout types. – Nicol Bolas Sep 06 '20 at 01:00
  • @NicolBolas _Which means that if T1 has no base class and T2 has an empty base class, implementations are required to give it zero size_ "zero size" means that the pointer to the base class subobject have to compare equal to pointer to some member subobject? An implementation can use some bytes between member subobjects which otherwise would be padding bytes to "allocate" the base class subobject. This won't change the size of `T2` and an implementation can claim that the size of the base class subobject is not "really" zero. – Language Lawyer Sep 06 '20 at 01:28
  • @NicolBolas _In short: EBO is required for standard layout types_ In short: on typical implementations, zero-size base class subobject are "really" zero-size because it would be easier to implement it this way. And not because it is mandatory by the Standard. – Language Lawyer Sep 06 '20 at 01:33
  • @LanguageLawyer The sentence in question reads: "if the object is a base class subobject of a standard-layout class type with no non-static data members, it has zero size" . There ain't no wiggle room - "it has zero size", not "it may have zero size". – Igor Tandetnik Sep 06 '20 at 02:36
  • @IgorTandetnik "it has zero size" means only that it may have the same address as some other subobject (https://timsong-cpp.github.io/cppwp/intro.object#9.sentence-2) (but it doesn't have to). On a hypothetical implementation, addition of a zero size base class can always increase the size of the complete object and this would be conforming. – Language Lawyer Sep 06 '20 at 02:45
  • LanguageLawyer: This topic is controversial. See @Jonathan Wakely's [answer](https://stackoverflow.com/a/10789707/2337207) in SO. – Alexander Sep 07 '20 at 10:34

0 Answers0