|
From: | Paolo Bonzini |
Subject: | Re: [PATCH] test-stddef: test offsetof compliance |
Date: | Tue, 17 Aug 2010 01:37:16 +0200 |
User-agent: | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.10) Gecko/20100621 Fedora/3.0.5-1.fc13 Lightning/1.0b2pre Mnenhy/0.8.3 Thunderbird/3.0.5 |
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
[Prev in Thread] | Current Thread | [Next in Thread] |