invalid offsetof from non-POD type
Nathan Sidwell
nathan@codesourcery.com
Fri Apr 25 12:14:00 GMT 2003
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.
yeah, I understand the idiom. what you really want is a pointer to void data
member. One way to get what you want is something like
#define myoff(T,m) ((size_t)(&((T *)1)->m) - 1)
that
a) circumvents g++'s invalid offsetof check
b) allows m to be from a base, and DTRT if the base is not at offset zero
c) DTWT for virtual bases and reference members
BTW, do people do things like
offsetof (T, m.sub_m)
? I don't think it blessed by the C/c++ std, but falls out of the common
implementation of offsetof.
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
The voices in my head said this was stupid too
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org
More information about the Gcc
mailing list