Const ref and rvalues again...

Jonathan M Davis jmdavisProg at gmx.com
Wed Nov 7 19:06:46 PST 2012


On Thursday, October 18, 2012 05:07:52 Malte Skarupke wrote:
> What do you think?

Okay. Here are more links to Andrei discussing the problem:

http://forum.dlang.org/post/4F83DBE5.20800@erdani.com
http://www.mail-archive.com/digitalmars-d@puremagic.com/msg44070.html
http://www.mail-archive.com/digitalmars-d@puremagic.com/msg43769.html
http://forum.dlang.org/post/hg62rq$2c2n$1@digitalmars.com

He is completely convinced that letting rvalues bind to const& in C++ was a 
huge mistake, and it seems to come down mainly to this:

"The problem with binding rvalues to const ref is that once that is in place 
you have no way to distinguish an rvalue from a const ref on the callee site."

And apparenly it required adding the concept of rvalue references to C++, 
which complicated things considerably.

It's too bad that he hasn't replied to some of the more detailed questions on 
the matter in this thread, but if you want rvalues to bind to const ref in D, 
you're going to have to convince him.

At this point, I expect that the most likely solution is that auto ref will 
continue to work like it does in templates but for non-templated functions it 
will become like const ref is in C++ and accept rvalues without copying 
lvalues. Andrei suggests that in at least one of those posts, and AFAIK, it 
would work just fine. The only real downside that I'm aware of is that then the 
semantics of auto ref are slightly different for non-templated functions, but 
you're doing basically the same thing with auto ref in both cases - avoiding 
copying lvalues and only copying rvalues when you have to - so it really 
shouldn't be a problem. The main problem is that someone needs to go and 
implement it.

- Jonathan M Davis


More information about the Digitalmars-d mailing list