invalid offsetof from non-POD type

Joe Buck jbuck@synopsys.com
Tue Apr 22 19:39:00 GMT 2003


On Tue, Apr 22, 2003 at 02:31:40PM -0400, Daniel Jacobowitz wrote:
> > There are cases where offsetof() could not possibly return a meaningful
> > value; in particular, for virtual base classes, the compiler must in
> > general dereference one or more pointers to get to the field, so there
> > is no fixed offset between the class object address and the field address
> > (in GCC's implementation of a virtual base class, the derived class object
> > must follow a pointer to get to the base class fields).
> 
> Is there an obvious reason why it would not be correct to do exactly
> this?  i.e. follow the virtual base pointers in order to establish the
> result of offsetof?

The typical use of offsetof is to create a transformation: given a
struct, I can map it to a field by just adding the offset to the pointer
to the base.  The problem is, when you have a virtual base class, this
transformation is broken.  Given a pointer to Foo, it might really point
to a class derived from Foo, and in this derived class, the distance to
a given field in the base class will be different.

For non-virtual derivation, the offset has the correct value even for
derived classes.

> [Is offsetof considered to be an integer constant expression?  I have
> no idea and I don't have the standard available.]

I believe that it is.  But even if it's only an integer expression, and
you try to compute it at runtime and use it later, it won't work right
for virtual bases, because the value sometimes



More information about the Gcc mailing list