const ref and rvalues

Jonathan M Davis jmdavisProg at gmx.com
Thu Feb 16 08:36:31 PST 2012


On Thursday, February 16, 2012 16:12:51 Timon Gehr wrote:
> On 02/16/2012 11:07 AM, Don Clugston wrote:
> > On 16/02/12 06:38, Martin Nowak wrote:

> >> C++'s 'const &' is very easy to get wrong.
> > 
> > Yes, but D still hasn't got it right.
> > As far as I can tell, "const ref" in a parameter list is ALWAYS a bug.
> 
> Why? It is perfectly reasonable to pass a large struct by const ref.

Because then you _have_ to have a variable to call the function - except for 
the bizarre situation that struct literals have where they're considered 
lvalues (very bad idea IMHO). I think that from the perspective of most 
programmers, the fact that const ref doesn't take rvalues is a major negative, 
even if Andrei is ultimately right (though I don't even remember what his 
reasoning is - I'm not sure that I've ever understod it).

The current solution is to use auto ref, but that only works with templates (I 
don't know if that's the plan or just a bug of the current implementation). 
So, the current situation is definitely broken.

If auto ref is changed to work with non-templated functions, then _why_ would 
you ever use const ref? You end up with a function that will only work with 
lvalues but where you don't actually intend to change the original. So, you 
probably end up with functions being either auto ref (or const auto ref) or 
ref with no const ref, making const ref ultimately rather pointless.

- Jonathan M Davis


More information about the Digitalmars-d mailing list