On 08/16/2010 11:52 PM, Paul Eggert wrote:
On 08/16/10 23:16, Eric Blake wrote:
The problem is how do we provide a working offsetof replacement, given
that there is no way using standard C89 or C++ constructs to do it? All
implementations either use something like this which abuses undefined
behavior within the standard but works without warning for that compiler:
#define offsetof(__a,__b) ((size_t)(&(((__a*)0)->__b)))
That's a perfectly reasonable replacement, for compilers
where offsetof is broken. It works for all such compilers that
we know of. If some new compiler comes up, we can deal with it
then.
Agreed. GCC only provides the builtin for three reasons, all related to
C++: 1) because the definition above is not an integral constant
expression, while offsetof's result is; 2) to support templates properly
(which it doesn't anyway, see http://gcc.gnu.org/PR17310), and 3) to
warn and/or fail when given strange C++ constructs involving overloaded
operator[].
In C, the macro will work in practice with all compilers. That said, if
the only problem we find is a non-parenthesized body, that is not huge.
Paolo