Discussion: Rvalue refs and a Move construtor for D

Suleyman sahmi.soulaimane at gmail.com
Wed Sep 4 17:41:02 UTC 2019

On Wednesday, 4 September 2019 at 16:37:30 UTC, Suleyman wrote:
> On Wednesday, 4 September 2019 at 14:56:03 UTC, kinke wrote:
>> On Wednesday, 4 September 2019 at 14:45:34 UTC, Suleyman wrote:
>>> 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
>>> }
>>> ```
>> Nope, this calls the copy ctor (https://godbolt.org/z/Ds1vmQ) 
>> without an explicit `T var = std::move(arg)`. This is part of 
>> what makes C++ rvalue refs difficult to grasp.
> Sorry I was mistaken. You're right. But still move information 
> is preserved with rvalue ref and it's utilizable for achieving 
> what Exil referred to. Ex: https://godbolt.org/z/dlesXb.

Apparently that's why C++' `forward` only moves at the end of the 
chain. Unlike `auto ref` in D which has to move at each level all 
the way down to the target. C++ Ex: https://godbolt.org/z/S1wacd.

More information about the Digitalmars-d mailing list