opAssign work not with initialisation?

Adam Burton adz21c at gmail.com
Sun Dec 19 06:40:15 PST 2010


spir wrote:

> On Sat, 18 Dec 2010 13:08:14 +0000
> Adam Burton <adz21c at gmail.com> wrote:
> 
>> > struct S {
>> >     int value;
>> >     void opAssign(int value) {
>> >         this.value = value;
>> >     }
>> > }
>> > unittest {
>> >     S s1;
>> >     s1 = 3;	// OK
>> >     S s2 = 3;   // _build_  error
>> > }
>> > ==>
>> > Element.d(105): Error: cannot implicitly convert expression (3) of type
>> > int to S
> 
>> That compiles for me. I am running 2.050 on linux.
> 
> Does not with dmd 2.049 on (ubuntu) Linux. If it's solved in 2.050, then
> let us let down.
> 
Actually this is my bad, I forgot to add the unittest switch :-P. Still 
happens in 2.050.
>> As a work-around try "S
>> s2 = S(3)", failing that then try adding a constructor that accepts an
>> int (opAssign is only used during assignment, not construction).
> 
> ;-) Sure, but this bypasses opAssign and assigns .value directly, I guess.
> What do you think?
Yes it will. AFAIK that is how it is supposed to work, opAssign is used for 
assignment to existing variables and the constructor or initialisation is 
used when creating it. So even if the above compiled you wouldn't get your 
call to opAssign in "S s2 = 3".
> 
> Denis
> -- -- -- -- -- -- --
> vit esse estrany ☣
> 
> spir.wikidot.com



More information about the Digitalmars-d-learn mailing list