LDC 0.16.0 beta2 is out! Try out before we create the final release!

Mike Parker via digitalmars-d-ldc digitalmars-d-ldc at puremagic.com
Wed Oct 14 02:02:40 PDT 2015


On Tuesday, 13 October 2015 at 11:22:54 UTC, Marco Leise wrote:

>
> I was of the impression that you need to put the initialized 
> field of a union first, but maybe I'm wrong. There is at least 
> this in the documentation on struct literals: "If there are 
> anonymous unions in the struct, only the first member of the 
> anonymous union can be initialized with a struct literal, and 
> all subsequent non-overlapping fields are default initialized."

You're correct in that only the first member of a union can be 
initialized. DMD will give you an error about "overlapping 
initialization" if you attempt to initialize a different member. 
However, it will allow you to do so if turn off default 
initialization for preceding members as in the Pastebin example.

union Foo {
     uint ui;
     float f = 2.0f;  //  Error: overlapping initialization for 
field f and ui
     double d;
}

union Bar {
     uint ui = void;    // This makes it OK
     float f = 2.0f;
     double d;
}

The pastebin example compiles, prints 256, and passes the assert 
with DMD 2.068.2. But I can't find any documentation on it 
anywhere.



More information about the digitalmars-d-ldc mailing list