D's equivalent to C++'s std::move?
Ola Fosheim Grøstad via Digitalmars-d
digitalmars-d at puremagic.com
Wed Feb 10 16:54:21 PST 2016
On Thursday, 11 February 2016 at 00:32:11 UTC, Matt Elkins wrote:
> unique owner falls out of scope. This situation occurs a lot
> for me, and RAII plus move semantics are pretty close to ideal
> for handling it. Yes, it can be approximated with reference
> counting, but reference counting has its own downsides.
C++ unique_ptr is a semantically a reference-counting ptr with a
max count of 1.
So, when you are using a shared_ptr, you also use move semantics.
Meaning, you can transfer one shared_ptr to another without the
overhead which would be a "move", or you can "copy" by increasing
the count by one.
> C++11 definitely supports this use case. I -think- that D does
> as well, possible compiler issues aside, though I haven't used
> it as extensively here to be sure yet.
In C++ it is a type system issue, and the actual semantics are up
to the programmer. In D it is just copy and clear, which does
extra work and is less flexible _and_ forces the copying to
happen so you cannot escape it. In C++ the type system tells the
class what it _may_ do, not what has already happened (which is
what D does). In C++ you aren't required to move, you can choose
to use a different strategy/semantics.
Like, in C++ you could do:
"take_one_but_not_both(std::move(a),std::move(b))"
That would not work in D.
More information about the Digitalmars-d
mailing list