invalid offsetof from non-POD type

Gabriel Dos Reis gdr@integrable-solutions.net
Fri Sep 12 04:55:00 GMT 2003


Arnold Cole <y09kpa@yahoo.com> writes:

|                                       In my mind,
| offsetof stands just on the same level as sizeof.

sizeof applies uniformly to all data type.

offsetof operates at a different level: that of datatypes with no
nearly no abstraction layer.

[...]

| #define offsetof(cls,member) (((size_t)&((cls
| *)0x100->member)-0x100)

it seems you have your own idea of what offsetof should mean for a
non-POD class.  That idea does not agree with mine.  For example,
given the above implementation of offsetof, what does it return for

   #include <iostream>
   struct A {
     int& r;
     A(int& i) : r(i) { }
   };

   int main()
   {
      std::cout << offset(A, r) << std::endl;
   }

on my machine (i686-pc-linux-gnu) I get a SIGSEV.

What about
 
    struct B  {
      enum { E = offset (A, r) };
    };

?

leave alone polymorphic classes.

[...]

| If any of the standard gurus out there would care to
| explain to me why offsetof shouldn't work for non POD
| types (and what POD types are), I'd appreciate it.

the issue has been hashed here and there in the past.

Anyway, it has been raised in the C++ standards committee.

-- Gaby



More information about the Gcc mailing list