Can you move a disabled this(this) struct in to a container type if it's an rvalue?

Paul Backus snarwin at gmail.com
Thu Dec 13 17:34:34 UTC 2018


On Thursday, 13 December 2018 at 13:17:05 UTC, aliak wrote:
>
> Ah. Is there any case where you would not want to do that when 
> you have a T value as parameter?
>
> And, what if it's "this()(auto ref T value)"? Then moving could 
> be dangerous if the parameter was passed as a ref. Or maybe it 
> just wouldn't compile?

Here's a version that moves rvalues and either copies lvalues, or 
fails to compile if it can't:

struct Container(T) {
     T value;
     this(T value) {
         import std.algorithm.mutation : move;
         this.value = move(value);
     }
     import std.traits : isCopyable;
     static if (isCopyable!T) {
         this(ref T value) {
             this.value = value;
         }
     }
}

Full example: https://run.dlang.io/is/0dw6zz


More information about the Digitalmars-d-learn mailing list