http://wiki.dlang.org/DIP25

Brad Anderson via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 31 10:00:13 PST 2014


On Wednesday, 31 December 2014 at 05:28:31 UTC, Andrei 
Alexandrescu wrote:
> The alternate hypothesis is "ref" is being misused. "ref" is 
> for propagating changes into the arguments. It should be rare 
> that code does not actually care for that. Unlike in C++, ref 
> is seldom needed for optimizing copies away. -- Andrei

Could you elaborate on that? Say I've got some big, expensive 
struct I want to pass into a function efficiently. In C++ I'd do 
this by const reference (or reference if I'm making changes).

What does D do that would make the ref unnecessary for 
efficiency? I can't imagine the compiler doing any sort of copy 
(even the efficient blig) being as fast as just passing in a 
reference. There is also a problem if the struct has disabled 
postblit. If the compiler silently turned something passed by 
value into a secret reference then it would open the door for 
problems during multithreading.

It is a shame that there is a conflation between an optimization 
and a tool for more flexible code structure. It makes it hard to 
express your intention when you use ref.

With DIP25 it seems like passing in an rvalue by ref would be 
safer and thus open it to being allowed.


More information about the Digitalmars-d mailing list