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

Lars T. Kyllingstad via Digitalmars-d digitalmars-d at puremagic.com
Sat Feb 13 01:18:40 PST 2016


On Thursday, 11 February 2016 at 00:32:11 UTC, Matt Elkins wrote:
>
> Maybe this is what you are referring to, but the primary use I 
> get out of move semantics (in general, not language-specific) 
> has little to do with performance-on-copy. It is for handling 
> resources which logically aren't copyable, which have a unique 
> owner at all times and which should be cleaned up as soon as 
> 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.

This is my primary use case for move semantics too, in both C++ 
and D.  Using noncopyable structs for this is perfect, because 
you can keep them on the stack and avoid heap allocation and 
indirection if you don't need to move them around a lot.  If you 
do move them often enough that D's copy-and-clear semantics 
become a performance issue, just stick them in something like 
std.typecons.Unique or std::unique_ptr.  Finally, if you need to 
have multiple owners/references, put them in a 
std.typecons.RefCounted or std::shared_ptr.

Best of all worlds.

Lars




More information about the Digitalmars-d mailing list