This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: bug with offsetof in egcs-1.1.2


On Apr 21, 1999, Jason Molenda <jsm@cygnus.com> wrote:

> 	class morething : public things {
> 	  static size_t OffsetTwo() { return offsetof(morething, two); }
> 	};

> gets the error message "t.cxx:18: invalid reference to NULL ptr, use
> ptr-to-member instead" trying to compile the OffsetTwo() function.  I
> *think* this is valid C++; the official rules for offsetof() and POD-structs
> are irritatingly dense, but there are no pointers-to-members in the
> structures here, so I think these are legal inputs to offsetof.

Nope.  morething isn't POD because it isn't an aggregate because it
has a base class.  Your code is not valid C++.  It *could* be
accepted, though, because no virtual inheritance is involved, so the
offset *is* a compile-time constant.  But then, why would you want to
write non-portable C++? :-)

It might work to define OffsetTwo() like this:

  static size_t OffsetTwo(morething *This = 0) {
    return ((char*)&(This->two)) - (char*)This;
  }

It would certainly be optimized away by any reasonable compiler, but
it would not be an integral constant, and, in theory, it may produce
undefined results, since it may dereference a null pointer.  But it
might be enough for you.

-- 
Alexandre Oliva http://www.dcc.unicamp.br/~oliva IC-Unicamp, Brasil
{oliva,Alexandre.Oliva}@dcc.unicamp.br  aoliva@{acm.org,computer.org}
oliva@{gnu.org,kaffe.org,{egcs,sourceware}.cygnus.com,samba.org}
*** E-mail about software projects will be forwarded to mailing lists



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]