2

Given the following example:

class A
{
    protected:
        static void useful_function_without_side_effects() {...}
}

class B : private A
{
    // B has no friends :(

    public:
        void travel_back_in_time() { super_useful_function(); }
}

Question 1: Would it be allowed for the compiler to optimize the base class A out, since this base class can't really affect B or it's run-time behaviour in any way?

Question 2: Would this change if the inheritance would be declared private virtual like this?

class B : private virtual A
Jack Sabbath
  • 1,398
  • 2
  • 9
  • 12
  • See [Empty base optimization](http://en.cppreference.com/w/cpp/language/ebo) or [empty-base-class-optimization](http://stackoverflow.com/questions/2826589/empty-base-class-optimization) – Jarod42 Apr 14 '15 at 13:10
  • @Jarod42: Thanks for the references. But despite them and the references back into the standard from cppreference.com, I'm unable to find a self-contained normative text about EBO. Lots of non-normative notes though. – Cheers and hth. - Alf Apr 14 '15 at 13:24
  • @Cheersandhth.-Alf: It's normative by omission: "Complete objects and member subobjects of class type shall have nonzero size" from [class]/4, with a note to clarify that base subobjects aren't so constrained, so can have zero size. But this question doesn't seem to require this level of pedantry. – Mike Seymour Apr 14 '15 at 13:29

1 Answers1

1

Answer 1:

Non-polymorphic classes have completely no representatives in runtime at all. The only things that may exist are objects and methods, which are seen as functions. The class only instructs the compiler to access parts of the object and resolve method calls the way as defined. Once resolved directly, everything is hard-coded in the runtime code. The private qualifier doesn't change anything here.

The A class (that has no fields) derived by B does not "add size" to the object of class B, should that be your question. This is always true if A class has no fields, but sizeof(A) will be always 1 at least. Although there's also no such rule that the size of B must be a sum of sizes of all fields and base classes.

Answer 2

This adds size to the B class. The standard doesn't precise what way. In typical implementations it will always extend the size of the B class by a size of one pointer, plus any possible size of class A.

Typically virtual inheritance is implemented with the use of "pointer to itself". That is, the subobject of derived class (A) is physically part of the overall object, but it's never accessed directly, but through a pointer in the overall object.

This is more-less the situation when you have fieldless A and B with fields of total size 4:

Physical inheritance:

B: [A: 0] [B extension: 4]

Virtual inheritance:

B: [A virtual: <pointer size>] [B extension: 4] [A shared subobject: 1]

The order of these things may differ between implementations, although it's part of the ABI definition, not compiler's private rule (that is, all compilers on one platform must use the same rules).

Ethouris
  • 1,791
  • 13
  • 18
  • Re "This is always true if A class has no fields", no, only if the first non-static data member in the derived class is of a type that is neither A nor a class derived from A. – Cheers and hth. - Alf Apr 14 '15 at 13:34
  • Re "Answer 2 This adds size to the B class.", that's *possibly* so, but you'd have to cite chapter and verse of the standard for that claim. – Cheers and hth. - Alf Apr 14 '15 at 13:36
  • Re #1: Field of type 'A' in class B will add at least size 1 to the class. This is because the pointer to one field cannot be equal to the pointer of another field, just like it's with pointers to objects. – Ethouris Apr 14 '15 at 13:45