Optimize my code =)

Stanislav Blinov stanislav.blinov at gmail.com
Sun Feb 16 16:19:26 PST 2014


On Sunday, 16 February 2014 at 23:47:08 UTC, Robin wrote:

> I am not currently aware of the "move semantics" in D or what 
> it is called there. However, I am not quite sure if D does an 
> equally good job as C++ in determining if a value can be moved 
> or not. I have made some simple tests which showed me that the 
> swap-assignment and the move-constructor are never called in D 
> at code where C++ would certainly take them, instead D just 
> copies values which were in fact rvalues ... (Maybe I just 
> don't get the whole thing behind the scenes - and I am really 
> hoping it.)

What swap assignment, what move constructor? :)

Here are the rules for moving rvalues in D:

1) All anonymous rvalues are moved;
2) Named temporaries that are returned from functions are moved;
3) That's it :) No other guarantees are offered (i.e. compilers 
may or may not try to be smart in other scenarios).

Sometimes when you want an explicit move you can use 
std.algorithm.move function. But keep in mind that its current 
implementation is incomplete (i.e. it doesn't handle immutability 
properly).

This code illustrates it all:

http://dpaste.dzfl.pl/c050c9fccbb2

> @All of you:
> What I have done since the last time:
> I have changed my mind - matrix is now a value type. However, I 
> wasn't sure about this step because I planned to implement 
> dense and sparse matrices which could inherit from a basic 
> matrix implementation in thus required to be classes.

Templates, perhaps?


More information about the Digitalmars-d-learn mailing list