[cxx-abi-dev] Empty Classes and data layout

John McCall rjmccall at apple.com
Thu May 9 18:10:21 UTC 2013


On May 9, 2013, at 9:35 AM, Richard Earnshaw <rearnsha at arm.com> wrote:
> We've been looking into a defect that's been raised on our C++ Binding
> for ARM relating to the handling of empty classes, that is, something like
> 
> struct S {};
> 
> In C this is not legal, and our C ABI defines no mechanism for passing
> such an object as a function parameter.  However, in C++ this is valid
> and at least at first reading matches the definition of a POD class.
> 
> However, the C++(98) states (clause 9 [class] para 3) that complete
> objects and member sub-objects of class type have non-zero size; which
> means that such a class, despite matching the GC++ABI rule for a POD for
> the Purposes of Layout definition fails to meet the condition in clause
> 2.2 that "All of these types have data size and non-virtual size equal
> to their size", since the size is one, but the data-size is zero.

This isn't a condition, it's a statement:  it specifies that the data size and
non-virtual size of these types is equal to their size.  This is necessary
because those values are otherwise computed by the layout algorithm,
and, well, we don't run the layout algorithm on types that are POD for
the purposes of layout.

The data size and non-virtual size of an empty class are ignored by the
layout algorithm.  When laying out a data member, only sizeof(D) and
alignof(D) are ever considered.  When laying out an *empty* base class,
only nvalign(D) (assumed to be 1) is ever considered.

The empty base class optimization applies to types regardless of whether
they're POD for the purposes of layout.  Being POD only affects the sizeof
vs. dsize/nvsize distinction.  This is permitted because base class
subobjects are explicitly permitted to have zero size, and the rules for
copying in and out of them are different.

I agree that a clarification is in order because the base C ABI doesn't
necessarily specify a layout for empty classes.  We should specify that
empty classes have a size of 1 by definition.

John.


More information about the cxx-abi-dev mailing list