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

w0rp via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 10 12:42:29 PST 2016


Back on the original topic, Scott Meyers often says "std::move 
doesn't move." It's more like std::rvalue_cast. C++ uses r-value 
references in order to be able to rip the guts out of objects and 
put them into other objects.

D doesn't have a distinct r-value reference type, and postblit is 
part of the struct types. If you write simply T foo(); and you 
return some struct T, D already moves the struct out of the 
function without you having to define any move constructors. That 
kind of return value optimisation was the original motivation for 
r-value references, for when C++98 RVO isn't good enough, from my 
understanding.

The only remaining time you need to avoid copies is when you take 
something already on the stack, and then put it into some other 
object, into some collection, etc. That's the other power that 
std::move affords you. The move functions in std.algorithm should 
take care of that.

Maybe someone else will correct me on a point or two there, but 
that's the understanding of move semantics in D that I have had.


More information about the Digitalmars-d mailing list