auto ref and non-templated functions
deadalnix
deadalnix at gmail.com
Wed Dec 26 22:12:00 PST 2012
On Thursday, 27 December 2012 at 05:53:23 UTC, Jonathan M Davis
wrote:
> No, it's not.
The example IS a const bug and have nothing to do with ref. You
get a mutable reference from a literal, which is immutable in the
first place.
> A function which takes its argument by ref is specifically
> intended to mutate its argument, otherwise it wouldn't take the
> argument by
> ref. Allowing such a parameter to be passed an rvalue makes it
> so that the
> function does not do what it's intended to do and will easily
> cause bugs.
>
A function can use ref argument for performance reasons.
> C++ made it so that const& would accept rvalues with the idea
> that if it were
> const, then you obviously didn't want to mutate the argument,
> and you could
> then use it as a means to avoid unnecessary copies. But with
> how restrictive
> const is in D, we really should have a means of declaring a
> function parameter
> such that it's intended to avoid unnecessary copies but isn't
> necessarily
> const and is obviously not intended to mutate the original
> (though as long as
> the parameter isn't const, mutation is obviously still a
> possibility, and
> const is required to guarantee that that doesn't happen -
> _that_ could be a
> const-related bug). But allowing ref functions to accept
> rvalues is an
> incredibly bad idea, because it's mixing two very different
> idioms - taking an
> argument by ref in order to mutate that argument and possibly
> taking an
> argument by ref in order to avoid a copy. It never makes sense
> to take an
> rvalue by ref. rvalue arguments need to use moves, not ref.
>
I have read this argument many any times, but still have to
experience it in actual code.
But, thinking about it, isn't the need for an auto ref type
necessary more than annotating every single function argument
with ref/auto ref ?
More information about the Digitalmars-d
mailing list