c++gccabstract-base-class

What is a "nearly-empty" class?


Compile the following class

class Interface
{
  virtual void doIt() = 0;
  virtual ~Interface() = 0;
};

inline Interface::~Interface() {}

using gcc -fdump-class-hierarchy.

gcc emits

Class Interface
   size=4 align=4
   base size=4 base align=4
Interface (0x1a779c0) 0 nearly-empty
    vptr=((& Interface::_ZTV9Interface) + 8u)

What is the significance of "nearly-empty"? What does it mean?


Solution

  • The C++ ABI provides a definition of "nearly empty" classes and an interesting discussion of how they affect vtable construction:

    A class that contains a virtual pointer, but no other data except (possibly) virtual bases. In particular, it:

    • has no non-static data members other than zero-width bitfields,
    • has no direct base classes that are not either empty, nearly empty, or virtual,
    • has at most one non-virtual, nearly empty direct base class, and
    • has no proper base class that is empty, not morally virtual, and at an offset other than zero.

    I ran across this while researching the effect of nearly empty virtual bases on object size, vtable size, and virtual call overhead.