D's equivalent to C++'s std::move?
Lars T. Kyllingstad via Digitalmars-d
digitalmars-d at puremagic.com
Sat Feb 13 01:11:06 PST 2016
On Wednesday, 3 February 2016 at 15:56:48 UTC, Ola Fosheim
Grøstad wrote:
> On Wednesday, 3 February 2016 at 15:44:25 UTC, Sönke Ludwig
> wrote:
>> seems like pretty clear semantics. And in C++ you'd have the
>> same situation once somefunction decides to move/swap the
>> value somewhere else before throwing an exception.
>
> Well, you can always move it back or wait with the move.
>
> Also, std.move may end up being inefficient when you have a
> complicated resource holder. Since the work is done before
> calling the function the optimizer may struggle with getting
> rid of the work.
In my experience, in the vast majority of cases a C++ move
operation boils down to a memberwise copy (of value types) or
copy-and-reset (of reference types). With the extra logic and
program flow that is sometimes involved in move construction and
move assignment, I suspect that a straightforward double memcpy
as it is done in D will be almost as performant or moreso most of
the time.
Add to that the fact that a lot of programmers out there will
implement move construction in terms of move assignment -- which
makes it a default construction PLUS move -- and move assignment
in terms of swap -- i.e., three moves -- for the sake of DRY.
Personally, I think D's move semantics are actually clearer and
easier to get right.
It is somewhat unfortunate that you cannot provide the strong
exception guarantee for a function when you move arguments into
it, though, but the semantics are pretty clear and easy to
explain to newbies: If you use std.move() on something it
definitely gets moved. In C++, if you use std::move() on
something it may or may not be moved; it depends on the recipient
of the move.
Lars
More information about the Digitalmars-d
mailing list