Thread overview
Can you move a disabled this struct in to a container type if it's an rvalue?
Dec 12, 2018
aliak
Dec 12, 2018
Paul Backus
Dec 13, 2018
aliak
December 12, 2018
Ie:

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

struct Container(T) {
    T value;
    this(auto ref 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);
}

But is there a way to do it without adding a custom constructor type?

Cheers,
- Ali

December 12, 2018
On Wednesday, 12 December 2018 at 20:05:18 UTC, aliak wrote:
> Ie:
>
> struct S {
>     @disable this();
>     this(int i) {}
> }
>
> struct Container(T) {
>     T value;
>     this(auto ref T value) {
>         this.value = value;
>     }
> }
>
> void main() {
>     auto a = Container!S(S(3)); // can't do this.
> }

The only error I get when I compile this has to do with incorrect use of `auto ref`. If I change the constructor's signature to `this()(auto ref T value)`, it works fine.
December 13, 2018
On Wednesday, 12 December 2018 at 21:11:38 UTC, Paul Backus wrote:
> On Wednesday, 12 December 2018 at 20:05:18 UTC, aliak wrote:
>> Ie:
>>
>> struct S {
>>     @disable this();
>>     this(int i) {}
>> }
>>
>> struct Container(T) {
>>     T value;
>>     this(auto ref T value) {
>>         this.value = value;
>>     }
>> }
>>
>> void main() {
>>     auto a = Container!S(S(3)); // can't do this.
>> }
>
> The only error I get when I compile this has to do with incorrect use of `auto ref`. If I change the constructor's signature to `this()(auto ref T value)`, it works fine.

Crap! So sorry, this was supposed to be this(this) and that auto ref is not supposed to be there :(

Posted new (and correct) message: https://forum.dlang.org/thread/sasdsmxmikxqfxhtbpyo@forum.dlang.org