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

Matt Elkins via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 10 17:45:32 PST 2016


On Thursday, 11 February 2016 at 00:54:21 UTC, Ola Fosheim 
Grøstad wrote:
> 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.

True, but with unique_ptr the max count is enforced by the 
compiler and can only be subverted by a programmer explicitly 
choosing to do so -- if that is possible with normal reference 
counting, I don't know of a way.

Moreover, there is no heap allocation required which may or may 
not matter for a given use case. Of course there are ways to 
avoid or mitigate heap allocations for reference-counted 
pointers, but the point is that unique_ptr has the next best 
thing to no overhead at all, which allows it to be used in a 
broader range of contexts.

> 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.

Fair point.



More information about the Digitalmars-d mailing list