From tejblum@developer.yandex.ru  Mon Jan 10 19:40:42 2005
Return-Path: <tejblum@developer.yandex.ru>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id CA91216A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 10 Jan 2005 19:40:42 +0000 (GMT)
Received: from developer.yandex.ru (developer.yandex.ru [213.180.193.15])
	by mx1.FreeBSD.org (Postfix) with ESMTP id A3B6E43D1F
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 10 Jan 2005 19:40:41 +0000 (GMT)
	(envelope-from tejblum@developer.yandex.ru)
Received: from developer.yandex.ru (localhost [127.0.0.1])
	by developer.yandex.ru (8.13.1/8.13.1) with ESMTP id j0AJedQT017491
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 10 Jan 2005 22:40:39 +0300 (MSK)
	(envelope-from tejblum@developer.yandex.ru)
Received: (from tejblum@localhost)
	by developer.yandex.ru (8.13.1/8.13.1/Submit) id j0AJedbx017490;
	Mon, 10 Jan 2005 22:40:39 +0300 (MSK)
	(envelope-from tejblum)
Message-Id: <200501101940.j0AJedbx017490@developer.yandex.ru>
Date: Mon, 10 Jan 2005 22:40:39 +0300 (MSK)
From: Dmitrij Tejblum <tejblum@yandex-team.ru>
Reply-To: Dmitrij Tejblum <tejblum@yandex-team.ru>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: FreeBSD's definition of offsetof isn't good for C++
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         76069
>Category:       gnu
>Synopsis:       FreeBSD's definition of offsetof isn't good for C++
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jan 10 19:50:04 GMT 2005
>Closed-Date:    Tue Mar 04 11:19:53 UTC 2008
>Last-Modified:  Tue Mar 04 11:19:53 UTC 2008
>Originator:     Dmitrij Tejblum
>Release:        FreeBSD 5.3-STABLE i386
>Organization:
>Environment:
System: FreeBSD developer.yandex.ru 5.3-STABLE FreeBSD 5.3-STABLE #11: Sun Dec 12 06:00:53 MSK 2004 root@developer.yandex.ru:/opt/obj/usr/src/sys/DEVEL i386

>Description:
The good old definition of the offsetof macro, found in <sys/cdefs.h>, leads 
to various compile problems with G++ 3.4 as supplied by FreeBSD. These 
problems doesn't exists with "original" version of GCC, it has its own
stddef.h. E.g., GCC's stddef.h use special __offsetof__ extension.
>How-To-Repeat:
Example: the following C++ snippet compiles with original GCC, but doesn't 
compile with native FreeBSD GCC.

---------------offsetof.cpp-----
#include <cstddef>

struct A {
        int a, b;
};

enum e {
        aoff = offsetof(A, a)
};
------------------------------
Compile with
c++ -c offsetof.cpp
fails with
offsetof.cpp:8: error: a casts to a type other than an integral or enumeration type cannot appear in a constant-expression
offsetof.cpp:8: error: '->' cannot appear in a constant-expression
offsetof.cpp:8: error: &' cannot appear in a constant-expression
offsetof.cpp:9: error: enumerator value for `aoff' not integer constant

>Fix:

The definition of offsetof is basically taken from GCC's stddef.h

--- src/sys/sys/cdefs.h	Fri Aug 27 17:14:59 2004
+++ src/sys/sys/cdefs.h	Mon Jan 10 22:19:30 2005
@@ -240,7 +240,14 @@
  * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
  * require it.
  */
+#if defined(__cplusplus) && __GNUC_PREREQ__(3,4)
+#define __offsetof(type, field)					\
+    (__offsetof__ (reinterpret_cast <size_t>			\
+	(&reinterpret_cast <char &>				\
+	(static_cast<type *> (0)->field))))
+#else
 #define	__offsetof(type, field)	((size_t)(&((type *)0)->field))
+#endif
 #define	__rangeof(type, start, end) \
 	(__offsetof(type, end) - __offsetof(type, start))
 
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: vwe 
State-Changed-When: Tue Mar 4 01:18:50 UTC 2008 
State-Changed-Why:  
This has been fixed in cvs src/sys/sys/cdefs.h rev. 1.88 
for RELENG_6 and up. 
Do you think we can close this PR? Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=76069 
State-Changed-From-To: feedback->closed 
State-Changed-By: vwe 
State-Changed-When: Tue Mar 4 11:19:14 UTC 2008 
State-Changed-Why:  
submitter agrees to close. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=76069 
>Unformatted:
