On Tuesday, 20 December 2016 at 20:08:32 UTC, Ali Çehreli wrote:

If the purpose is optimization, the good news are

Yes it is :)

* Classes are already reference types so there is no lvalue or rvalue reference distinction there

Ok, this one is quite intuitive.

import std.stdio;
...

Thank you for the illustrative example, I have reproduced it.

There is a surprising difference in D:

In D, non-constness of an object seems to be more important in overload resolution: Notice how mutable lvalue above is passed to by-copy instead of the potentially-more-optimal by-const-ref above. D realizes that a mutable object is for mutation and because by-const-ref cannot mutate it, D passes it to the by-copy function. (This may be seen as a bug by some.)

Thank you for pointing out this. I was not aware of that, and for sure this is not the C++ behavior.

Interestingly, enabling the by-mutable-ref overload above, now the mutable object goes to by-ref and there is no automatic copy:

Ok, that is "moral" and without surprise.

--- rvalue ---
constructor       1
foo(by-copy)      1
destructor for    1

--- mutable lvalue ---
constructor       2
foo(by-ref)       2
destructor for    2

--- const lvalue ---
constructor       3
foo(by-ref-const) 3
destructor for    3

Ali

[1] I have an issue with "rvalue reference" as rvalue references can be references to lvalues as well. :p

Thank you for your time and these valuable explanations, I learnt a lot.
--Vincent

Reply via email to