rvalue references

Jonathan M Davis jmdavisProg at gmx.com
Sat Apr 27 18:40:36 PDT 2013


On Saturday, April 27, 2013 21:17:07 Andrei Alexandrescu wrote:
> On 4/27/13 8:17 PM, Jonathan M Davis wrote:
> > Walter himself has said several times in
> > the past that the solution used for auto ref and templates can't work with
> > non-templated functions.
> 
> I think he's partly wrong about that. Yes, the same mechanics won't
> work, but the same outcome can be made to work.

Yes. I agree. It's how auto ref is implemented for templated functions which 
won't work, not the idea. As I've described here and elsewhere, it could be 
done by simply making it so that auto ref translated to ref underneath the 
hood with the difference that when an rvalue was passed to it, a variable would 
be created on the stack which the rvalue would be assigned to so that there 
would be an lvalue to pass to the function by ref. As long as the lifetime of 
the variable matches that of the statement that the function, then it should 
be fine.

But the question here was why we couldn't just use the same solution for auto 
ref as templated functions with non-templated functions, and I think that it's 
fairly clear that that won't work as that relies on being able to generate 
additional functions. It also creates a combinatorial explosion of functions, 
so heavily using auto ref for templated functions could create a lot of 
unnecessary code bloat, which is why it would be so desirable to be able to 
use the non-templated functions solution for auto ref with template functions 
(either by introducing a new attribute - either for what auto ref does for 
templated functions now or for the new auto ref implementation - or by doing 
what Timon suggested and treating the new auto ref implementation as an 
optimization for templated functions, but that does require the compiler to 
have some smarts).

- Jonathan M Davis


More information about the Digitalmars-d mailing list