This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
offsetof "problem"
- From: Andrew Pollard <andrewp at andypo dot net>
- To: gcc at gcc dot gnu dot org
- Date: Sun, 31 Mar 2002 02:49:31 +0100
- Subject: offsetof "problem"
Hi All,
A checkin by "Jason Merrill"...
>date: 2002/02/01 19:31:20; author: jason; state: Exp; lines: +26 -4
> * typeck.c (build_component_ref): Always complain about offsetof
> constructs on non-PODs. Only make it an error for members of
> virtual bases.
I realise that there must be a good reason behind this, but.....
How are you meant to define things that get the size of an arbitary
member, or the offset of a member.... eg
#define OFFSET_OF(type,field) \
(reinterpret_cast <size_t> \
(reinterpret_cast <char *> \
(&(reinterpret_cast <type *> (0))->field)))
#define SIZEOF_MEMBER(type,field) \
(sizeof ((reinterpret_cast <type *> (0))->field))
now whinge with a warning.... (gcc-3.1)
I realise that these were always a bit of dodgy code... but now all
get out of gcc-3.1(20020329) are
> warning: invalid offsetof from non-POD type `struct A'; use
> pointer to member instead
Fair enough, but the types in question are basically POD types, but
they just have member functions and constructors/destructors.
I can understand that in the presence of inheritance, etc, that these
macros could be dodgy, but is there a 'proper' way of doing them?
Thanks,
Andrew.
--
Andrew Pollard, ASI/Brooks Automation | home: andrew@andypo.net
670 Eskdale Road, Winnersh Triangle, UK | work: Andrew.Pollard@brooks.com
Tel/Fax:+44 (0)118 9215603 / 9215660 | http://www.andypo.net