Discussion: Rvalue refs and a Move construtor for D

Suleyman sahmi.soulaimane at gmail.com
Wed Sep 4 14:45:34 UTC 2019


On Wednesday, 4 September 2019 at 00:33:06 UTC, Exil wrote:
> How would it work with multi-function passing though? With a 
> rvalue reference, you are effectively just passing around a 
> reference, until the contents of the value are moved. So you 
> can pass it through N functions and it won't ever to do a 
> move/copy.
>
> Would you effectively be relying on the compiler to optimize 
> out the un-necessary moves, or would they be unavoidable, as 
> they effectively are now?

I think you scored a valid point here. This is where having 
rvalue ref comes in handy. In C++ assigning an rvalue ref to an 
lvalue does move not copy.

C++ example:
```
void fun(T&& arg)
{
     T var = arg; // move not copy
}
```

D:
```
void fun(ref T arg)
{
     T var = arg; // copy not move
}
```

Ref doesn't preserve move information about the source. If we go 
the `@rvalue ref` way then we can achieve something similar.


More information about the Digitalmars-d mailing list