1

Possible Duplicate:
Why can’t you use offsetof on non-POD strucutures in C++?

I have two classes:

struct A{
  double one;
  float two;
};

struct B : public A{
  float three;
};

If I interpretete the C++11 standard layout restrictions correctly, then B is not standard layout, because both B and A have non-static data members. I would like to use a construct like B in combination with HDF5, which requires the use of the offsetof macro, which (to my knowledge) is restricted to standard-layout objects. My question is, what could go wrong, with an expression like

offsetof(B, three)

It works fine with gcc-4.6, but has anybody ever encountered unexpected behaviour in a similar situation? If so, which one and with which compiler? What is the worst case scenario?

Regards Claas

Community
  • 1
  • 1
Claas
  • 389
  • 1
  • 3
  • 5
  • I have read this one too, but the examples of offsetof failures all contain constructs which are much more difficult than the example above. That's why I am curious about the chances of the example above to show unexpected behaviour. – Claas Oct 30 '12 at 08:11
  • The standard says directly (§18.2/4): "*If type is not a standard-layout class, the results are undefined.*" You really want a definition for "undefined"? – ildjarn Oct 30 '12 at 18:03
  • No, I was actually not looking for a definition of "undefined". I completely agree, that the code above is not backed by the standard and thus should not be used. Out of curiosity, however, I was wondering, whether there exists at least one implementation of the standard, which causes the above example to fail. So it was more a practical than theoretical question. – Claas Jan 29 '13 at 08:27

0 Answers0