[Issue 19917] unions should require that all members are `= void` initialised

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 29 05:11:24 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=19917

Nicholas Wilson <iamthewilsonator at hotmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |iamthewilsonator at hotmail.co
                   |                            |m

--- Comment #1 from Nicholas Wilson <iamthewilsonator at hotmail.com> ---
The way it currently works is the first member of a union is the one that
dictates the .init of the union

struct S
{
  union
  {
    int x;
    float y; // COMPILE ERROR, 'x' already initialised!
  }

  union
  {
    int z;
    float w = void; // <- this is fine
  }
}

S s;
assert( (cast(int*)&s)[0 .. 2]) == [0,0]); // passes

reverse the order and the assert fails.

If you want void initialisation then if know (or can compute) the size of the
union (say N) then you can make the first member a `void[N] dummy;`.

If the docs need to make that more explicit do say so, go forth and complain on
the forums!

--


More information about the Digitalmars-d-bugs mailing list