The C++ standard (quoting from draft n3242) says the following about subobjects [intro.object]:
Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two distinct objects that are neither bit-fields nor base class subobjects of zero size shall have distinct addresses.
Now, given the following snippet:
struct empty { };
struct member: empty { };
struct derived: empty { member m; };
int main(void)
{
printf("%d", sizeof(derived));
return 0;
}
gcc I believe prints out 2
, and Visual C++ 2010 prints out 1
. I suspect gcc is taking the standard to mean you can't alias the storage of types if they represent different objects. And I bet MSVC is taking the standard to mean if one subobject is zero sized, you can do whatever you want.
Is this unspecified behavior?
member
is set tochar
. – Conchaconchieif (this != &that)
. – Affinaltwo subobjects that have the same class type and that belong to the same most derived object must not be allocated at the same address
– Silage