DIP 1016--ref T accepts r-values--Community Review Round 1

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Jul 20 15:40:17 UTC 2018


On Friday, July 20, 2018 15:18:33 Daniel N via Digitalmars-d wrote:
> On Friday, 20 July 2018 at 13:21:11 UTC, Jonathan M Davis wrote:
> > auto ref was already introduced to solve this problem. The
> > problem of course is that it requires that the function be
> > templated, and while that's often desirable, it's not always a
> > viable option (e.g. it doesn't work with virtual functions).
> > So, I'm fine with adding something akin to auto ref which is
> > intended to solve the non-templated case with semantics similar
> > to those described in the DIP, but I think that it would be a
> > huge mistake to make normal ref accept rvalues.
>
> I'm in favour of this DIP.
>
> However, after this DIP... what is the point of the old "auto
> ref"? If you want to turn your function into a template it's
> trivial.
>
> What if the current semantics of "auto ref" was redefined to what
> this DIP proposes, i.e. non-templated rvalue ref.

When auto ref is used, the refness of the arguments is forwarded, which can
be critical for stuff like emplace. It also avoids any temporaries, because
if you pass an rvalue, it just instantiates the template so that the
parameter isn't ref, resulting in a move, which is almost certainly more
efficent than what this DIP proposes. So, we would definitely not want to
change what auto ref means for templated functions.

We could choose to implement auto ref for non-templated functions using the
semantics proposed for ref in this DIP, but that would mean that auto ref
did different things for templated and non-templated code, which would be
connfusing and potentially problematic when trying to do something like
templatize an existing function. Also, it would mean that templates could
not use the new semantics, whereas if we added a new attribute such as
@rvalue, then both templated and non-templated functions could have
functions accept rvalues by ref using temporaries in those cases that the
programmer wants that behavior instead of auto ref (e.g. to reduce template
bloat).

- Jonathan M Davis



More information about the Digitalmars-d mailing list