rvalue references

Manu turkeyman at gmail.com
Fri Apr 26 17:21:32 PDT 2013


On 27 April 2013 05:31, Jonathan M Davis <jmdavisProg at gmx.com> wrote:

> 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.
>

So you're saying it should be const ref instead of auto ref...
I agree.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130427/2f2f382e/attachment.html>


More information about the Digitalmars-d mailing list