Boost logo

Boost :

From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2007-10-11 17:46:44

Roland Schwarz wrote:
> Altough I am not terribly skilled in standards interpretation I think
> the following should be safe:
> union { mem[]; foo s; };
> Now I should be able to access this as mem[0], mem[1], ... from the
> writing side, e.g. from a function that fills my buffer, and later
> access it with s.a, s.b.
Absolutely not. The only circumstance in which you may access a member
of a union that is not the one that has been most recently set is when
you access common, structurally conformant members of structs, i.e.

struct foo
  int i;
  float f;

struct bar
  int one;
  float two;

union foobar
  foo f;
  bar b;

foobar fb;
fb.f.i = 0;
fb.f.f = 1.1;

cout <<; // Yep, valid.

The moment the types are not exactly equal (and two consecutive chars
are not the same as a char[2]) you're no longer allowed to assume anything.

Sebastian Redl

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