Move semantics for D

Benjamin Thaut code at benjamin-thaut.de
Thu Jul 12 23:50:03 PDT 2012


Move semantics in C++0x are quite nice for optimization purposes. 
Thinking about it, it should be fairly easy to implement move semantics 
in D as structs don't have identity. Therefor a move constructor would 
not be required. You can already move value types for example within an 
array just by plain moving the data of the value around. With a little 
new keyword 'mov' or 'move' it would also be possible to move value 
types into and out of functions, something like this:

mov Range findNext(mov Range r)
{
   //do stuff here
}

With something like this it would not be neccessary to copy the range 
twice during the call of this function, the compiler could just plain 
copy the data and reinitialize the origin in case of the argument.
In case of the return value to only copying would be neccessary as the 
data goes out of scope anyway.

The only question would be if this causes any problems with out contracts.

The pre C++0x move trick, reserving a bit in the value representation 
for marking that the next copy is a move, is unfortunately not possible 
D because D does not have a copy constructor.

I for example have a range that iterates over a octree and thus needs to 
internally track which nodes it already visited and which ones are still 
left. This is done with a stack container. That needs to be copied 
everytime the range is copied, which causes quite some overhead.

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d mailing list