Annoying thing about auto ref function template

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 20 14:53:47 PDT 2017


On Monday, March 20, 2017 21:37:26 Yuxuan Shui via Digitalmars-d wrote:
> On Monday, 20 March 2017 at 21:34:14 UTC, Yuxuan Shui wrote:
> > On Monday, 20 March 2017 at 21:08:40 UTC, Jonathan M Davis
> >
> > wrote:
> >> [...]
> >
> > This is a bit tedious because it requires you creating a new
> > function.
> >
> > Maybe we can create a template for that. But still, auto ref
> > requires us to do things differently, which is annoying.
>
> Easy solution: just support auto ref for non-templates.
>
> I think someone has already did work on that?

auto ref for non-templates would not be quite the same thing, and
regardless, it wouldn't help any with explictly instantiating a template
that had an auto ref parameter. So, it really wouldn't solve the problem at
all. It would just make it so that if you didn't want a templated function,
you could use auto ref.

But for auto ref to work with non-templated functions, it would either
result in creating a combinatorial explosion of functions rather than just a
single function (basically doing what auto ref does but explicitly
instantiating it with every combination of refness instead of just the ones
that are actually used), or it would just create a version where every auto
ref parameter was ref and silently copy rvalues to the stack to pass by ref
when they're passed to the function. I think that there was a PR on the
issue at one point, and IIRC, the ultimate decision was to reject it and
keep the status quo, because there were too many downsides it (e.g. IIRC,
there were problems with overriding functions in derived classes).

I think that there was some talk semi-recently about creating a proposal for
something similar to C++'s const & but which was more restrictive in order
to avoid the downsides with C++'s solution, in which case we might end up
with something like @rvalue ref which accepted rvalues - probably by copying
them to the stack first. But I don't think that anything has been formally
proposed yet. If that goes anywhere, it will probably be the closest that
you'll ever get to having auto ref with non-templated functions.

- Jonathan M Davis



More information about the Digitalmars-d mailing list