invalid offsetof from non-POD type

Gabriel Dos Reis gdr@integrable-solutions.net
Tue Apr 22 11:07:00 GMT 2003


Nathan Sidwell <nathan@codesourcery.com> writes:

[...]

| Like Zack, I think it strange that I can write &thing.member but not
| offsetof(thing, member) (modulo virtual bases). I can even write
| '(char *)&thing.member - (char *)&thing', and all sane compilers will
| evaluate it at compile time.

I don't know whether you take GCC to be a "sane" compiler, but it
can't evaluate this at compile-time -- and if it does, then I would say
it is broken.

  #include <iostream>
  #include <cstddef>

  struct A {
     double d;
     int& ri;
     A(double v, int& i) : d(v), ri(i) { }
  };

  int main()
  {
     int i = 9;
     A a(3.4, i);

     std::cout << "(char*)&a.i - (char*)&a = " << ((char*)&a.ri - (char*)&a)
               << std::endl;
  }

That expression may be negative (and it is so on some platforms).

I think, we need a better technical term and criterion than "sane".

-- Gaby



More information about the Gcc mailing list