invalid offsetof from non-POD type

Paul Koning pkoning@equallogic.com
Fri Apr 25 15:13:00 GMT 2003


>>>>> "Nathan" == Nathan Sidwell <nathan@codesourcery.com> writes:

 Nathan> John Quigley wrote:
 >> Perhaps it would be useful if I described how the code that I'm
 >> working on uses offsetof.  It is the Torque game engine from
 >> www.garagegames.com.  The engine has an internal c-like scripting
 >> language.  The fields and methods of the c++ classes in the engine
 >> can be exposed to this scripting language.  For each field, the
 >> engine uses offsetof to compute the offset of the field in the
 >> class.  Later on, when the scripts read or write to the field, the
 >> script runtime locates the field by adding the offset to the base
 >> pointer of the object in question.

 Nathan> yeah, I understand the idiom. what you really want is a
 Nathan> pointer to void data member. One way to get what you want is
 Nathan> something like

 Nathan> #define myoff(T,m) ((size_t)(&((T *)1)->m) - 1)

 Nathan> that
 Nathan> a) circumvents g++'s invalid offsetof check
 Nathan> b) allows m to be from a base, and DTRT if the base is not at
 Nathan> offset zero
 Nathan> c) DTWT for virtual bases and reference members

I noticed that seems to work, but I'd rather have a
-Wno-offsetof-null-check to suppress the warning.  That way our builds
(which are -Wall -Werror) will work again without having to hack the
code with ever more bizarre hacks.

 Nathan> BTW, do people do things like offsetof (T, m.sub_m)

I think so, yes.  Certainly I would expect that to work just as well
as the "regular" offsetof().

   paul



More information about the Gcc mailing list