OSDN Git Service

.:
authornathan <nathan@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 22 Apr 2003 12:28:51 +0000 (12:28 +0000)
committernathan <nathan@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 22 Apr 2003 12:28:51 +0000 (12:28 +0000)
* ginclude/stddef.h: Provide C++ safe offsetof.
testsuite:
* g++.dg/other/offsetof2.C: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@65936 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/ginclude/stddef.h
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/other/offsetof2.C [new file with mode: 0644]

index 9965b90..d5b8370 100644 (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
index f1046f4..ad091ea 100644 (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 */
 
index 577a609..8328263 100644 (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.
diff --git a/gcc/testsuite/g++.dg/other/offsetof2.C b/gcc/testsuite/g++.dg/other/offsetof2.C
new file mode 100644 (file)
index 0000000..3ab6398
--- /dev/null
@@ -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;
+}
+