struct default constructor, unions and bizaro behavior.

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 11 08:20:33 PST 2014


On 12/10/14 4:57 PM, deadalnix wrote:
> struct S {
>       union {
>           T1 t1;
>           T2 t2;
>       }
>
>       T3 t3;
> }
>
> T1 a1;
> T3 a3;
> S(a1, a3);
>
> This is erroring because t1 is set twice. It turns out that the
> second parameter of the struct map to t2 rather than t3.
>
> This behavior do not make any sense, ever. Why is that the
> default ?

I'm not sure it's reasonable to expect this much out of the 
compiler-generated default ctor. It probably just does the equivalent of 
this.tupleof[0..args.length] = args;

Can't you just add a constructor for it?

-Steve


More information about the Digitalmars-d mailing list