rvalue references

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 2 11:19:28 PDT 2015


On Tuesday, 2 June 2015 at 18:05:20 UTC, Namespace wrote:
> AFAIK Andrei wanted 'auto ref' as the syntax which accepts 
> both, lvalues and rvalues. That's why I'm asking if the current 
> behaviour for auto ref for templates should change, or not. If 
> not, we have (as you said) two meanings of auto ref, what is 
> not optimal, but not so bad either (IMO).
> But if I had the choice, I would change it for both, so that 
> both create a temporary variable for rvalues.

Andrei originally wanted auto ref to apply to non-templated 
functions, but Walter misunderstood what he meant and created 
what we have now - which is actually incredibly useful, because 
it allows you to forward the attributes of an argument - in 
particular, it's ref-ness. So, we definitely do _not_ want to 
lose that.

>> So, basically, I think that we have three options:
>>
>> 1. Do nothing. If you want a function parameter to accept both 
>> lvalues and rvalues efficently, then either duplicate it with 
>> various overloads to achieve that or templatize it and use 
>> auto ref so that the compiler will do that for you.
> But that means we get (at worst) 2^N functions. And note that 
> each function contains the whole body, not just a call to one 
> of the previous creations.

Yes. auto ref with templated functions results in a lot of 
template bloat if you use it heavily, which is part of why using 
auto ref with non-templated functions at this point isn't a 
particularly ideal solution, not unless we could actually make it 
so that the compiler could optimize the extraneous instantiations 
and use a temporary value for the rvalues when the attribute 
forwarding isn't actually needed, but I wouldn't really expect us 
to get that.

- Jonathan M Davis


More information about the Digitalmars-d mailing list