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

Boris-Barboris ismailsiege at gmail.com
Thu Dec 13 12:08:22 UTC 2018


On Thursday, 13 December 2018 at 09:51:42 UTC, aliak wrote:
> Ie:
>
> struct S {
>     @disable this(this);
>     this(int i) {}
> }
>
> struct Container(T) {
>     T value;
>     this(T value) {
>         this.value = value;
>     }
> }
>
> void main() {
>     auto a = Container!S(S(3)); // can't do this.
> }
>
> I can build a custom constructor for Container that makes this 
> work:
>
> static auto construct(Args...)(auto ref Args args) {
>     import std.algorithm: move;
>     auto value = T(args);
>     auto opt = Container!T.init;
>     opt.value = move(value);
>     return move(opt);
> }
>
> And then "auto a = Container!T.construct(3);" works.
>
> But is there a way to do it without adding a custom constructor 
> type?
>
> Cheers,
> - Ali


You can just move in container constructor:


struct S {
     @disable this(this);
     this(int i) {}
}

struct Container(T) {
     T value;
     this(T value) {
         import std.algorithm: move;
         this.value = value.move;
     }
}

void main() {
     auto a = Container!S(S(3));
}


More information about the Digitalmars-d-learn mailing list