stddef.h: Provide C++ safe offsetof.

.:	* ginclude/stddef.h: Provide C++ safe offsetof.
testsuite:
	* g++.dg/other/offsetof2.C: New test.

From-SVN: r65936
This commit is contained in:
Nathan Sidwell 2003-04-22 12:28:51 +00:00 committed by Nathan Sidwell
parent 834277a89a
commit ad27e65a22
4 changed files with 62 additions and 1 deletions

View file

@ -1,3 +1,7 @@
2003-04-22 Nathan Sidwell <nathan@codesourcery.com>
* ginclude/stddef.h: Provide C++ safe offsetof.
2003-04-22 J"orn Rennecke <joern.rennecke@superh.com>
* function.c (purge_addressof_1): In (mem (addressof (reg))) case

View file

@ -410,8 +410,14 @@ typedef __WINT_TYPE__ wint_t;
#ifdef _STDDEF_H
/* Offset of member MEMBER in a struct of type TYPE. */
#ifndef __cplusplus
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#else /* C++ */
/* The reference cast is necessary to thwart an operator& that might
be applicable to MEMBER's type. See DR 273 for details. */
#define offsetof(TYPE, MEMBER) (reinterpret_cast <size_t> \
(&reinterpret_cast <char &>(static_cast <TYPE *> (0)->MEMBER)))
#endif /* C++ */
#endif /* _STDDEF_H was defined this time */

View file

@ -1,3 +1,7 @@
2003-04-22 Nathan Sidwell <nathan@codesourcery.com>
* g++.dg/other/offsetof2.C: New test.
2003-04-21 Mark Mitchell <mark@codesourcery.com>
* g++.dg/template/recurse.C: Adjust location of error messages.

View file

@ -0,0 +1,47 @@
// { dg-do run }
// { dg-options -Wold-style-cast }
// Copyright (C) 2003 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 22 Apr 2003 <nathan@codesourcery.com>
// DR273 POD can have an operator&, offsetof is still required to work
#include <stddef.h>
struct POD1
{
int m;
void *operator& () const {return 0;} // yes, still a pod!
};
struct POD2
{
int m;
};
void *operator& (POD2 const &) {return 0;} // ouch!
struct POD3
{
int prefix;
POD1 m;
};
struct POD4
{
int prefix;
POD1 m;
};
int main ()
{
if (offsetof (POD3, m) != sizeof (int))
return 1;
if (offsetof (POD4, m) != sizeof (int))
return 2;
return 0;
}