DIP 36: Rvalue References

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 23 11:22:03 PDT 2013


On Tue, 23 Apr 2013 14:01:14 -0400, Diggory <diggsey at googlemail.com> wrote:

>
>> So no more returning ref?  Because if you allow returning ref, you lose  
>> any notion of safety, unless you plan on changing the entire  
>> compilation model?
>
> The rules from DIP25/35 show how you can return refs while still  
> maintaining safety.

Those rules disallow the following valid code:

struct S
{
     int x;
     ref S opOpAssign(string op : "+")(ref S other) { x += other.x; return  
this;}
}

ref S add5(ref S s)
{
     auto o = S(5);
     return s += o;
}

void main()
{
     auto s = S(5);
     S s2 = add5(s);
}

Because opOpAssign takes two refs to S, and in add5, we bind one parameter  
to a local, we cannot return the result, even though it's perfectly safe  
and valid.

The point is simply that there exist valid and safe cases which will be  
disallowed by these rules.  While maybe it's OK in your mind to restrict  
the above code from validity, it's currently valid and compiling code.   
You will be breaking existing code for pretty much no reason.

-Steve


More information about the Digitalmars-d mailing list