rvalues -> ref (yup... again!)

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Mar 24 07:04:00 UTC 2018


On Saturday, March 24, 2018 01:37:10 Nick Sabalausky  via Digitalmars-d 
wrote:
> On 03/23/2018 07:46 PM, Jonathan M Davis wrote:
> > On Friday, March 23, 2018 22:44:35 Nick Sabalausky via Digitalmars-d 
wrote:
> >> It never made any sense to me that there could be any problem
> >> with the compiler automatically creating a temporary hidden
> >> lvalue so a ref could be taken. If there IS any problem, I can
> >> only imagine it would be symptomatic of a different, larger
> >> problem.
> >
> > It can be a serious API problem if you can't look at ref and know that
> > the intention is that the original argument's value will be used and
> > then mutated (whereas with out, it will be mutated, but the original
> > value won't be used).
>
> ???. That's equally true when an lvalue is passed in.
>
> > However, that could be solved by having a different attribute indicate
> > that the idea is that the compiler will accept rvalues and create
> > temporaries for them if they're passed instead of an lvalue. Then, the
> > situation is clear.
> Why require the callee's author to add boilerplate? Just do it for all
> ref params that are given an rvalue.

Because if the point of the function accepting its argument by ref is
because it's going to mutate the argument, then it makes no sense for it to
accept rvalues, whereas if the point of the function accepting its argument
by ref is to just avoid a copy, then it makes sense to accept both. It
should be clear from the API which is intended. As it stands, because a
function can't accept rvalues by ref, it's usually reasonable to assume that
a function accepts its argument by ref because it's mutating that argument
rather than simply because it's trying to avoid a copy. If ref suddenly
starts accepting rvalues, then we lose that.

With C++, you know the difference because, only const& accepts rvalues, and
whether it's const or some other attribute, I'd very much like that any ref
that accepts rvalues in D be marked with something to indicate that it does
rather than making ref do double-duty and make it unclear what it's there
for.

- Jonathan M Davis



More information about the Digitalmars-d mailing list