how to compute offset of data member at compile time (virtual class)

Andrew Haley aph-gcc@littlepinkcloud.COM
Thu Oct 25 18:59:00 GMT 2007


John Gateley writes:
 > On Thu, 25 Oct 2007 17:57:11 +0100
 > Andrew Haley <aph-gcc@littlepinkcloud.COM> wrote:
 > 
 > > John Gateley writes:
 > >  > I need to compute the offset of a data member at compile time.
 > >  > The class has virtual functions, and so the offsetof macro does
 > >  > not work (why?).
 > > 
 > > Because it's a non-POD type.  See Section 9, Classes:
 > > 
 > >  > Is there a way to do this?
 > > 
 > > No.  Consider what happens with multiple and virtual inheritance.
 > > 
 > 
 > Thanks for the reply.
 > 
 > It seems to me that even with multiple
 > and virtual inheritance, the offset is a compile time constant.
 > It may be complex to determine it, but it doesn't change.

Well, that depends on the implementation.  With gcc and other
compilers that follow the multi-vendor C++ ABI, things are
better-defined than the standard actually requires.  So, while what
you describe may indeed be possible, it isn't legal ISO C++.

Andrew.



More information about the Gcc-help mailing list