Nearly empty classes

Mark Mitchell mark at codesourcery.com
Thu Oct 24 04:19:36 UTC 2002


It's been pointed out that this definition:

 nearly empty class

     A class that contains no data except its virtual pointer (if any)
     or 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. 

isn't quite right.

In particular, you can read that, in conjunction with:

  If C has a dynamic base class, attempt to choose a primary base
  class B. It is the first (in direct base class order) non-virtual
  dynamic base class, if one exists. Otherwise, it is a nearly empty
  virtual base class, the first one in (preorder) inheritance graph
  order which is not an indirect primary base class if any exist, or
  just the first one if they are all indirect primaries.

to say that in this situation:

  struct S {};
  struct T : virtual public S {};

S is the primary base of T.

The problem is that S is nearly-empty by the above definition; that
definition doesn't actually *require* the nearly-empty base to have a
virtual pointer.  Of course, it makes no sense to have a primary base
that does not have a virtual pointer; in fact, following the layout
rules would then indicate that T has no virtual pointer even though it
does have a virtual base.

I believe the correct fix is to to rephrase the first line of the
definition of nearly empty class as:

  A dynamic class that contains no data except its virtual pointer or
  virtual bases.  In particular, it:

Any objections?

--
Mark Mitchell                   mark at codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com



More information about the cxx-abi-dev mailing list