rvalue references
Jonathan M Davis
jmdavisProg at gmx.com
Tue Apr 23 19:56:26 PDT 2013
On Tuesday, April 23, 2013 14:58:50 Andrei Alexandrescu wrote:
> On 4/23/13 2:45 PM, Namespace wrote:
> >> auto ref is needed to accept rvalues.
> >
> > I'm curious as to when that is implemented. Or if I purely watch here
> > again in a year and the issue still exists. :)
>
> It is implemented, but currently only for templates.
I think that the obvious thing to do for auto ref and non-templates is to just
make it so that underneath the hood it's ref, and if you pass an rvalue to it,
a variable is created on the stack (presumably for the lifetime of the
statement that the function call is in) and passed to the function (so then
it's an lvalue as ref requires).
However, the problem with simply making auto ref do this for non-templated
functions is that then it functions fundamentally differently for templated and
non-templated functions. And we can't change auto ref to work that way for
templated functions, because auto ref's current behavior has proven useful for
forwarding the refness of an argument (which auto won't do on its own), which
improves D's forwarding capabilities. But given the combinatorial explosion of
template instantiations if you have many auto ref variables for a templated
function, most uses of auto ref with templated functions really should be
using what I just described for non-templated functions. But to allow both
behaviors, we pretty much need a new attribute. And if we're using a new
attribute, we either need to make it do what auto ref currently does for
templates (and therefore change auto ref to do the new thing and break some
amount of code out there), or we need to make the new attribute do the new
thing and never implement auto ref for non-templated functions.
So, probably the best route at this point is to come up with a new attribute
which is replace with ref in the function definition and transparently creates
variables for rvalues so that they can be passed to the function as lvalues,
and then that attribute works with both templated and non-templated functions.
But it _is_ kind of annoying to not be able to use auto ref for that given
that that's basically what it was intended for in the first place.
Maybe I'm missing something here, but that's how the situation seems to me.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list