advise about safety of using move in an opAssign with __traits(isRef

aliak something at something.com
Sun Aug 18 17:06:44 UTC 2019


On Saturday, 17 August 2019 at 16:43:51 UTC, Paul Backus wrote:
> On Friday, 16 August 2019 at 08:07:28 UTC, aliak wrote:
>> Hi, I'm trying to fix a use-case where you have a wrapper 
>> template type (it's an optional) and the wrapping type has 
>> @disable this(this). And having this scenario work:
>>
>> struct S {
>>   @disable this(this);
>> }
>> Optional!S fun() {...}
>>
>> Optional!S a;
>> a = fun.move;
>>
>> Now that won't work because of the disabled copy
>
> Are you sure? I tried to reproduce the error you're describing, 
> and I couldn't do it. The following compiles and runs without 
> any issues:
>
> struct Wrapper(T) { T value; }
> struct NoCopy { @disable this(this); }
>
> Wrapper!NoCopy fun()
> {
>     return Wrapper!NoCopy();
> }
>
> void main()
> {
>     Wrapper!NoCopy a;
>     a = fun(); // already an rvalue, so moved implicitly
> }
>
> Can you give a more complete example of the problem you're 
> having?

You'll get the error if you define an opAssign:

struct Wrapper(T) {
     T value;
     void opAssign(U : T)(Wrapper!U other) {
         this.value = other.value;
     }
}

struct S {
   @disable this(this);
}

Wrapper!S fun() { return Wrapper!S(S()); }

void main() {
	Wrapper!S a;
	a = fun;
}




More information about the Digitalmars-d-learn mailing list