www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - struct default constructor, unions and bizaro behavior.

reply "deadalnix" <deadalnix gmail.com> writes:
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 ?
Dec 10 2014
next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 12/10/2014 1: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 ?
Please file a bug report.
Dec 10 2014
parent "deadalnix" <deadalnix gmail.com> writes:
On Thursday, 11 December 2014 at 03:33:01 UTC, Walter Bright 
wrote:
 Please file a bug report.
I did, kenji told me that this is as per design and expected. I don't think this design make any sense, so u bring the thing here :)
Dec 10 2014
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Dec 11 2014
parent reply "deadalnix" <deadalnix gmail.com> writes:
On Thursday, 11 December 2014 at 16:20:33 UTC, Steven
Schveighoffer wrote:
 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
I ran into this issue because of another bug that prevent the default constructor to be found in some template mixin madness piece of code :) Anyway, I think this is reasonable to have one element for the union in the tupleof as well.
Dec 11 2014
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 12/11/14 6:15 PM, deadalnix wrote:

 I ran into this issue because of another bug that prevent the
 default constructor to be found in some template mixin madness
 piece of code :)
Ah, template-mixin madness! Sounds contagious...
 Anyway, I think this is reasonable to have one element for the
 union in the tupleof as well.
Which one then? :) -Steve
Dec 12 2014
parent "deadalnix" <deadalnix gmail.com> writes:
On Friday, 12 December 2014 at 16:23:48 UTC, Steven Schveighoffer
wrote:
 Anyway, I think this is reasonable to have one element for the
 union in the tupleof as well.
Which one then? :)
One, having an union type.
Dec 12 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Wednesday, 10 December 2014 at 21:57:42 UTC, 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 ?
Isn't using named union instead an option?
Dec 11 2014