Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 03 Oct 2004 20:02:15 -0500
From:      Patrick Hartling <patrick@137.org>
To:        freebsd-current@freebsd.org
Subject:   offsetof() macro, C++, and GCC 3.4.2
Message-ID:  <1096851735.869.18.camel@dsl.80.187.networkiowa.com>

next in thread | raw e-mail | index | archive | help

--=-RQqghuUyuTunfNoIn3LL
Content-Type: multipart/mixed; boundary="=-9iN2Cm6WIjnvOh4dLc70"


--=-9iN2Cm6WIjnvOh4dLc70
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

The offsetof() macro defined in stddef.h evaluates to code that can
violate the C++ standard depending upon the context of its use.  I have
attached code that demonstrates the problem.  GCC 3.4.2 in the base
system refuses to compile the code, giving this error message:

% g++ -c const-exp.cpp
const-exp.cpp:14: error: a casts to a type other than an integral or
enumeration type cannot appear in a constant-expression
const-exp.cpp:14: error: '->' cannot appear in a constant-expression
const-exp.cpp:14: error: `&' cannot appear in a constant-expression

The gcc34 port (gcc-3.4.2_20040827) builds it just fine.  The
difference, as far as I can tell, is that gcc34 from the Ports
Collection gets offsetof() from its own stddef.h, which has C++-friendly
code:

/* Offset of member MEMBER in a struct of type TYPE. */
#ifndef __cplusplus
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#else
/* The cast to "char &" below avoids problems with user-defined
   "operator &", which can appear in a POD type.  */
#define offsetof(TYPE, MEMBER)                                  \
  (__offsetof__ (reinterpret_cast <size_t>                      \
                 (&reinterpret_cast <const volatile char &>     \
                  (static_cast<TYPE *> (0)->MEMBER))))
#endif /* C++ */

Would fixing this problem be as simple as using the above code
in /usr/include/stddef.h?

The attached code was adapted from some very similar code in
Boost.Python (from the file boost/python/object/instance.hpp), which
does not compile with the base system GCC 3.4.2.  There may be other
parts of Boost 1.31.0--and the upcoming Boost 1.32.0--that are affected
by this offsetof() problem.

 -Patrick


--=20
Patrick L. Hartling                     | Research Assistant, VRAC
http://www.137.org/patrick/             | 2274 Howe Hall Room 2624
PGP: http://tinyurl.com/2oum9           | http://www.vrac.iastate.edu/

--=-9iN2Cm6WIjnvOh4dLc70--

--=-RQqghuUyuTunfNoIn3LL
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (FreeBSD)

iD8DBQBBYKEXwZ1qaMh08hcRAhwlAKCiR2TrPIqyH/2a5Vlgsxqrl6pdkQCePMU6
QTKIKSzrRLiajgxLWdKTiJQ=
=bEPg
-----END PGP SIGNATURE-----

--=-RQqghuUyuTunfNoIn3LL--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1096851735.869.18.camel>