auto ref and non-templated functions
Jonathan M Davis
jmdavisProg at gmx.com
Wed Dec 26 14:51:37 PST 2012
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.
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
More information about the Digitalmars-d
mailing list