This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: invalid offsetof from non-POD type
Nathan Sidwell <nathan at codesourcery dot 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