auto ref function parameter causes that non copyable struct is copied?

tchaloupka chalucha at gmail.com
Tue Nov 9 06:15:47 UTC 2021


On Tuesday, 9 November 2021 at 02:43:55 UTC, jfondren wrote:
> On Tuesday, 9 November 2021 at 02:41:18 UTC, jfondren wrote:
>> The expectation is probably that `f.move` set `f` to 
>> `Foo.init`, but the docs say:
>
> Posted too fast. Foo qualifies with its @disable:
>
> ```d
> struct A { int x; }
> struct B { int x; @disable this(this); }
>
> unittest {
>     import core.lifetime : move;
>     auto a = A(5);
>     auto b = B(5);
>     a.move;
>     b.move;
>     assert(a == A(5));
>     assert(b == B(0));
> }
> ```

Yes it should qualify so it should be cleaned out when moved.
When I change:

```D
auto ref unwrap(EX)(auto ref EX res) {
     printf("unwrap()\n");
     return res.get();
}
```

I get:

```
~this(0)
~this(0)
~this(0)
unwrap()
~this(42)
~this(0)
~this(42)
```

So the destructor isn't called from `gen()` -> there is 
[NRVO](https://dlang.org/glossary.html#nrvo) in play.

Also `pragma(msg, __traits(isRef, res));` prints false in unwrap 
in this case (which is expected), but how it gets there as a 
value when it's not moved and instead copied even though it has 
disabled copy constructor?
`isCopyable!(Value!Foo)` returns false as expected too.


More information about the Digitalmars-d-learn mailing list