Const ref and rvalues again...

Jonathan M Davis jmdavisProg at gmx.com
Tue Nov 6 15:41:40 PST 2012


On Wednesday, November 07, 2012 00:32:48 Manu wrote:
> But it only really makes sense in the context of templates...?
> Why should something called 'auto ref' provide fabrication of temporaries
> for the purpose of passing rvalues to functions that receive ref args?
> 
> How does auto ref (under some different definition/implementation) address
> the rvalue problem?

The idea behind auto ref was to create a function which would take its 
argument in the most efficient way possible, whatever that was (be it ref or by 
value), and that the compiler would figure that out. That way, the function 
wouldn't care whether it was given an rvalue or lvalue or a value type or 
reference type or whatever. What actually got implemented was simply 
duplicating functions with auto ref so that there's a ref version of lvalues 
and non-ref versions for rvalues, which really doesn't do the job and only 
works with templates.

What we need is an attribute which indicates that you don't care whether the 
argument is an lvalue or rvalue. You just don't want it to be copied, and you 
don't want the function to mutate it - which is what const& in C++ is normally 
used for. Whether auto ref is the attribute used for that is more or less 
irrelevant except for the fact that that's the problem that it was trying to 
solve in the first place.

Making auto ref do that for non-templated functions would solve the problem 
for non-templated functions but would mean that auto ref on parameters in 
templated functions and non-templated functions would have different (albeit 
similar) semantics, which would be a bit of problem and may or may not be 
acceptable. And I'd have to think it through more to say whether or not auto 
ref as it is on templated functions really solves the problem well enough for 
templated functions or not. If it does, then it may be acceptable to just have 
auto ref on non-templated functions with slightly different semantics. If it 
doesn't, then we either need to change what auto ref does with templated 
functions to match what we want for non-templated ones (which could be a 
problem, since some people have found the current semantics of auto ref useful 
for other stuff like propagating attributes), or we'd need to use a new 
attribute rather than auto ref.

- Jonathan M Davis


More information about the Digitalmars-d mailing list