auto ref and non-templated functions

deadalnix deadalnix at gmail.com
Wed Dec 26 21:26:38 PST 2012


On Wednesday, 26 December 2012 at 22:52:29 UTC, Jonathan M Davis 
wrote:
> On Wednesday, December 26, 2012 23:02:25 deadalnix wrote:
>> Sound like the wayt o go for me. But is ato ref needed in such
>> case ? Why not simply allow ref to behave that way ?
>
> Because there's a very large difference between a function 
> intended to take
> arguments by ref and one where you don't care. For instance, 
> what if popFrontN
> suddenly accepted rvalues? Using it with rvalues would do 
> nothing. We had
> problems with std.conv.parse precisely because there was a 
> compiler bug that
> allowed you to pass string literals to it in spite of the fact 
> that it takes
> its argument by ref and needs to mutate its argument.
>

That is a const bug not a ref bug.

> With auto ref, you're specifically saying that you don't care 
> whether the
> function is given an lvalue or rvalue. You just want it to 
> avoid unnecessary
> copies. That's very different. And auto ref then not only then 
> protects you
> from cases of passing an rvalue to a function when it needs an 
> lvalue, but it
> makes it clear in the function signature which is expected.
>
> - Jonathan M Davis

I suspect making the distinction is complicating uselessly the 
language for a benefit that isn't really clear.


More information about the Digitalmars-d mailing list