The liabilities of binding rvalues to ref
Rob T
alanb at ucora.com
Thu May 9 16:01:23 PDT 2013
On Thursday, 9 May 2013 at 21:55:51 UTC, Manu wrote:
> Umm, what if the non-template counterpart returns ref? Then it
> doesn't
> behave the same.
Yes, the non-template version cannot ref return an auto ref
param, but I would expect that neither can the template version
unless auto ref is specified as the return value. The key
difference that I think can be understood is that auto ref on the
return value for a template is not the same thing as auto ref on
the param.
For templates, you can enforce exactly the same behavior as the
non-template version by specifying a return of either ref or not,
ie you do not specify auto ref or you specify a return by value.
If you so happen to specify a ref return of an auto ref param, I
would expect the compiler to refuse to compile no matter if it is
a template function or not. Of course you can confuse the
programmer and allow such a thing to compile only for the cases
where it can return ref on the auto ref, but IMO that'll be a big
mistake as it will confuse the s*** out of most people, and
besides allowing something like that has no value at all.
> D is a complex language, so stuff like this does take some
> getting used to,
>> but it is very powerful and flexible, no two ways around it.
>>
>
> If this takes 'getting used to', you're basically admitting
> that it doesn't
> make intuitive sense.
Well, I suppose I cannot disagree with you on that point, so yes
it is confusing, but for sake of a solution, it is not nearly as
confusing so long as auto ref on the parameters will behave the
same for both template and non-template versions in a consistent
way. I know auto ref on the return is potentially confusing, but
it does increase template flexibility as the benefit.
Alternatively, I think Jonathan's argument against scope ref
makes perfect sense. Unless I misread something or he's dead
wrong, how can scope be used without creating even more
confusion? Even if scope has some properties in common with auto
ref, it specifies something entirely different than accepting
rvalues and lvalues. This point reminds me of why we should not
be using bool as an integral type, it's the same reasoning.
--rt
More information about the Digitalmars-d
mailing list