Bug 11072 - [3.4 regression] Implementation of offsetof macro
Summary: [3.4 regression] Implementation of offsetof macro
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P1 critical
Target Milestone: 3.4.0
Assignee: Nathan Sidwell
URL:
Keywords:
Depends on:
Blocks: 10922
  Show dependency treegraph
 
Reported: 2003-06-03 01:34 UTC by Wolfgang Bangerth
Modified: 2004-01-17 04:22 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2003-06-03 16:45:03


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Wolfgang Bangerth 2003-06-03 01:34:39 UTC
[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)));
Comment 1 Ralf W. Grosse-Kunstleve 2003-06-03 02:29:02 UTC
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.)
Comment 2 Wolfgang Bangerth 2003-06-03 13:48:27 UTC
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/


Comment 3 Wolfgang Bangerth 2003-06-03 20:00:40 UTC
See also discussion about legality of the code here:
  http://gcc.gnu.org/ml/gcc-bugs/2003-06/msg00482.html
Comment 4 GCC Commits 2003-07-02 14:30:56 UTC
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

Comment 5 Nathan Sidwell 2003-07-02 14:40:08 UTC
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.
Comment 6 Nathan Sidwell 2003-07-02 14:40:43 UTC
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.