[Yet another problem with 10922. It is similar to 9881, which is why I CC: you, Nathan.] This fails on mainline: ----------------------------- #include <cstddef> template <typename> struct S { int i, j; static const unsigned int value = offsetof(S,j); }; ----------------------------- g/x> /home/bangerth/bin/gcc-3.3-pre/bin/c++ -c y.cc g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c y.cc y.cc:5: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression Now, in contrast to the claim in PR 10922, I can't find a place in the standard that would mandate that the result of offsetof is an integer constant expression. 18.1.5 only says that it can only be applied to POD types, which certainly is the case here. However, it might be a QoI issue to really let the result be an integral constant. W. PS: The expansion of offsetof yields here: (reinterpret_cast <size_t> (&reinterpret_cast <char &>(static_cast <S *> (0)->j)));
ISO/IEC 9899:1999 (E) 7.17 Common definitions <stddef.h> 3 The macros are NULL which expands to an implementation-defined null pointer constant; and offsetof(type, member-designator) which expands to an integer constant expression that has type ^^^^^^^^^^^^^^^^^^^^^^^^^^^ size_t, the value of which is the offset in bytes, to the structure member (designated by member-designator), from the beginning of its structure (designated by type). The type and member designator shall be such that given static type t; then the expression &(t.member-designator) evaluates to an address constant. (If the specified member is a bit-field, the behavior is undefined.)
Subject: Re: [3.4 regression] Implementation of offsetof macro > ------- Additional Comments From rwgk@yahoo.com 2003-06-03 02:29 ------- > ISO/IEC 9899:1999 (E) > > 7.17 Common definitions <stddef.h> > > 3 The macros are > > NULL > > which expands to an implementation-defined null pointer > constant; and offsetof(type, member-designator) which > expands to an integer constant expression that has type > ^^^^^^^^^^^^^^^^^^^^^^^^^^^ This provision is only in the C standard. The C++ standard has a different wording, which I believe is intentional given that 18.1.2 says The contents are the same as in the Standard C library header stddef.h, with the following changes: followed by definitions of NULL and offsetof. W. ------------------------------------------------------------------------- Wolfgang Bangerth email: bangerth@ices.utexas.edu www: http://www.ices.utexas.edu/~bangerth/
See also discussion about legality of the code here: http://gcc.gnu.org/ml/gcc-bugs/2003-06/msg00482.html
Subject: Bug 11072 CVSROOT: /cvs/gcc Module name: gcc Changes by: nathan@gcc.gnu.org 2003-07-02 14:30:53 Modified files: gcc : ChangeLog gcc/ginclude : stddef.h gcc/testsuite : ChangeLog gcc/testsuite/g++.dg/other: offsetof2.C Added files: gcc/testsuite/g++.dg/other: offsetof5.C Log message: PR c++/11072 * ginclude/stddef.h (offsetof): Remove cast to 'char &'. Explain why. testsuite: PR c++/11072 * g++.dg/other/offsetof2.C: XFAIL. * g++.dg/other/offsetof5.C: New. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.300&r2=2.301 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ginclude/stddef.h.diff?cvsroot=gcc&r1=1.18&r2=1.19 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2828&r2=1.2829 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/other/offsetof5.C.diff?cvsroot=gcc&r1=NONE&r2=1.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/other/offsetof2.C.diff?cvsroot=gcc&r1=1.1&r2=1.2
2003-07-02 Nathan Sidwell <nathan@codesourcery.com> PR c++/11072 * ginclude/stddef.h (offsetof): Remove cast to 'char &'. Explain why. 2003-07-02 Nathan Sidwell <nathan@codesourcery.com> PR c++/11072 * g++.dg/other/offsetof2.C: XFAIL. * g++.dg/other/offsetof5.C: New.