Boost logo

Boost :

From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2007-10-12 03:54:18

Roland Schwarz wrote:
> Hmm, you may have missed the details of my example, so I repeat the
> essentials:
> struct foo1
> {
> unsigned data[4];
> };
> struct foo2
> {
> unsigned data[2];
> };
> struct bar
> {
> foo1 a;
> foo2 b;
> };
> union { data[6]; bar b; };
> Are those types not "layout compatible" as the standard requires for the
> access I intend? I mean foo1, foo2 are POD. So the question boils down
> to whether foo1 and unsigned data[4] are layout compatible, not?
The answer is still no. The standard says (9.2/16):
> If a POD-union contains two or more POD-structs that share a common
> initial sequence, and if the POD-union object currently contains one
> of these POD-structs, it is permitted to inspect the common initial
> part of any of them. Two POD-structs share a common initial sequence
> if corresponding members have layout-compatible types (and, for bit-
> fields, the same widths) for a sequence of one or more initial mem-
> bers.
This is the only case where access through a different member is allowed.

unsigned data[6] is not a POD-struct, even though it is a POD.

Furthermore, you definitely can't access foo2's data, because there
might be padding between foo1 and foo2. (Not likely that any compiler
does that, but they're allowed to.)

Sebastian Redl

Boost list run by bdawes at, gregod at, cpdaniel at, john at