This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
using offsetof with g++
- From: Doug Evans <dje at transmeta dot com>
- To: gcc at gcc dot gnu dot org
- Date: Thu, 6 Nov 2003 10:09:23 -0800
- Subject: using offsetof with g++
version = 3.3.2, i386-linux
cp/typeck.c has this:
/* Complain about other invalid uses of offsetof, even though they will
give the right answer. Note that we complain whether or not they
actually used the offsetof macro, since there's no way to know at this
point. So we just give a warning, instead of a pedwarn. */
if (null_object_p && CLASSTYPE_NON_POD_P (object_type))
{
warning ("invalid access to non-static data member `%D' of NULL object",
member);
warning ("(perhaps the `offsetof' macro was used incorrectly)");
}
No doubt there's a language citation for why this exists,
but I wonder if you guys would be amenable to adding an option
to turn this check off. Is there some expectation that "the right answer"
will no longer be possible some day?
While a warning in and of itself isn't fatal, consider -Werror.
Also consider a c++ application that does dynamic compilation.
Any suggestions for how to get the offsetof various fields
that I can use in the code generator?
[other than rewriting of course]
Consider:
claire:~$ cat foo.cc
class b
{
public:
int b1;
};
#define B_OFFSETOF(field) ( (unsigned) (& ((b*)0)->field) )
class c : public b
{
public:
int c1;
};
#define C_OFFSETOF(field) ( (unsigned) (& ((c*)0)->field) )
unsigned
bfoo ()
{
return B_OFFSETOF (b1);
}
unsigned
cfoo ()
{
return C_OFFSETOF (c1);
}
-->
foo.cc: In function `unsigned int cfoo()':
foo.cc:27: warning: invalid access to non-static data member `c::c1' of NULL
object
foo.cc:27: warning: (perhaps the `offsetof' macro was used incorrectly)