Does D really need something like const&?

Era Scarecrow rtcvb32 at yahoo.com
Fri Mar 1 16:49:54 PST 2013


> On Fri, 01 Mar 2013 18:35:43 -0500, Namespace 
> <rswhite4 at googlemail.com> wrote:
>> At least I have noticed so far, that you're in any case for 
>> something like const&. Accordingly, you answer my question 
>> with 'yes, we need something like this.'.

On Friday, 1 March 2013 at 23:46:36 UTC, Steven Schveighoffer 
wrote:
> I would say yes, we need something like rvalue references to 
> avoid copy-paste hell.  const& is not a good way to describe 
> it, because it implies const, which this problem does not 
> require.
>
> This is the major problem that Andrei had with it (at least as 
> I understand his past statements) -- it conflates const with 
> rvalue references.  Sometimes, you want a const ref that does 
> NOT bind to an rvalue.

  If I have a medium sized struct that I only intend to 
read/reference from I see no reason not to use 'const ref' for 
simplicity and speed (and avoid postblit hopefully) but when it's 
an Rvalue I need to make a second function either duplicate 
except signature or forwarding the value.

> The one huge problem I've had with lack of rvalue references is 
> with arithmetic operators:
>
> struct M
> {
>   M opAdd(const ref M other) const {...}
> }
>
> M m;
>
> auto m2 = (m + m) + m; // ok!
> auto m3 = m + (m + m); // error!
>
> This is crap.

  If 'auto ref' gets accepted for non-template functions, it goes 
away. With M as you show, returning ref doesn't work so that 
example I was going to suggest doesn't work.


More information about the Digitalmars-d mailing list