D's equivalent to C++'s std::move?

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 11 09:38:11 PST 2016


On Thursday, 11 February 2016 at 16:31:03 UTC, Atila Neves wrote:
> On Thursday, 11 February 2016 at 14:38:24 UTC, Ola Fosheim 
> Grøstad wrote:
>> On Thursday, 11 February 2016 at 14:25:39 UTC, Atila Neves 
>> wrote:
>>> D has move semantics. Deep copies are done with post-blit. 
>>> Fair enough if you just:
>>>
>>> auto foo = bar;
>>>
>>> Then it's a shallow copy. The only difference to a "true" 
>>> move is that bar isn't T.init, but that's easily done with 
>>> the move function (assuming the struct has a destructor) or 
>>> manually.
>>
>> *blank stare*
>
> Err... ok.

I don't see how D's parameter semantics can be called move 
semantics, when you essentially can emulate it in C++ without 
using C++'s move semantics?

>> void foo(const T&) // copy semantics overload
>> void foo(T&&) // move semantics overload
>
> I forgot the const. It doesn't change my point.

The point is of course that you use (const T&) instead of 
copying, so you don't have to deal with the 
constructor/destructor overhead?

> No it doesn't. It _allows_ you to perfect forward, as long as 
> you remember to use `std::forward`. And in that case, they're 
> not really rvalue references, they're forwarding references 
> (what Scott Meyers initially called universal references).

Of course you have to use std::forward, that follows from what I 
said further up about how "T&&" parameters act when used.



More information about the Digitalmars-d mailing list