rvalue references
Jonathan M Davis
jmdavisProg at gmx.com
Fri Apr 26 12:31:29 PDT 2013
On Friday, April 26, 2013 14:15:07 Manu wrote:
> > I mean is that the way that auto ref should work with non-templated
> > functions
> > is that
> >
> > auto foo(auto ref T param) {...}
> >
> > becomes
> >
> > auto foo(ref T param) {...}
> >
> > underneath the hood. Then when you pass an rvalue to it - e.g. foo(T(5)) -
> > that gets translated to something like
> >
> > T __temp = T(5);
> > foo(__temp);
> >
> > Then auto ref works with both lvalues and rvalues with only one function
> > definition, and ref is unchanged in how it works (it still only accepts
> > lvalues).
>
> Why bother with 'auto'? Why not just make this default behaviour?
For the same reason that T& doesn't take rvalues in C++ while const T& does.
There's a big difference between wanting an argument to be passed as efficiently
as possible and specifically wanting to alter the argument being passed in.
Plain ref is for cases where you explicitly want to mutate the argument.
You're just asking for bugs if you allow ref to accept rvalues. We've had
problems like this before when some literals were treated as lvalues. The
behavior of a function which takes its argument by ref and the behavior of one
which takes its argument by auto ref are fundamentally different.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list