This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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:

<< A POD-struct is an aggregate class that has no non-static data members
of type pointer to member, non-POD-struct, non-POD-union (or array of
such types) or reference, and has no user-defined copy assignment
operator and no user-defined destructor. Similarly, a POD-union is an
aggregate union that has no non-static data members of type pointer to
member, non-POD-struct, non-POD-union (or array of such types) or
reference, and has no user-defined copy assignment operator and no
user-defined destructor. A POD class is a class that is either a
POD-struct or a POD-union. >>

Section 18.1 Para 5:

<< The macro offsetof accepts a restricted set of type arguments in
this International Standard. type shall be a POD structure or a POD
union (clause 9). The result of applying the offsetof macro to a field
that is a static data member or a function member is undefined. SEE
ALSO: subclause 5.3.3, Sizeof, subclause 5.7, Additive operators,
subclause 12.5, Free store, and ISO C subclause 7.1.6. 18.2
Implementation properties [lib.support.limits] >>

 > Is there a way to do this?

No.  Consider what happens with multiple and virtual inheritance.

Andrew.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]