Discussion: Rvalue refs and a Move construtor for D

Suleyman sahmi.soulaimane at gmail.com
Fri Sep 6 15:18:24 UTC 2019


On Thursday, 5 September 2019 at 22:57:27 UTC, kinke wrote:
> [...]

Seems workable. It only affects the case of moving an lvalue to 
an value parameter. But here are my observations:

1. `move` should always do what `forward` did in your example. It 
shouldn't touch the lvalue, for simply removing a call to the 
move constructor, because T.init is not always considered a valid 
state and resetting to T.init is a "move" operation but an 
uncontrolled one which leaves the object in an invalid state. 
Which why making __move behave just like rvalue ref is more 
sensible like your first attempt did. Ex:

```
     S lvalue;
     lvalue.i = 1;
     foo(__move(lvalue));
     assert(lvalue.i == ??); // could be still 1 if wasn't 
consumed inside foo. this is valid behavior with rvalue ref.
```

This behavior is better than swapping lvalue with T.init.

2. What you have proposed so far only replaces rvalue ref 
parameters. You haven't tackled rvalue ref returns. What do you 
propose as an alternative. Ex:

```
     @rvalue ref get(@rvalue ref S arg) { return arg; } // 
receives pointer and returns it
     void foo(@rvalue ref S);

     S lvalue;
     foo(get(lvalue)); // no copy or move happens only pointers 
being passed
```

What is your alternative for this.


More information about the Digitalmars-d mailing list