Binding rvalues to ref parameters redux

Steven Schveighoffer schveiguy at gmail.com
Fri Mar 29 22:56:44 UTC 2019


On 3/29/19 8:16 AM, Andrei Alexandrescu wrote:
> On 3/29/19 7:32 AM, Olivier FAURE wrote:
>> On Wednesday, 27 March 2019 at 01:38:40 UTC, Andrei Alexandrescu wrote:
>>> https://gist.github.com/andralex/e5405a5d773f07f73196c05f8339435a
>>>
>>> Thanks in advance for any feedback.
>>
>> - During discussions around DIP-1016, it was pointed out that the 
>> following code currently compiles, even though it really shouldn't:
>>
>>      struct Point
>>      {
>>          private int _x, _y;
>>          ref int x() { return _x; }
>>          ref int y() { return _y; }
>>      }
>>
>>      struct Rect
>>      {
>>          private Point _origin, _size;
>>          Point origin() { return _origin; }
>>          Point size() { return _size; }
>>          void origin(Point p) { _origin = p; }
>>          void size(Point p) { _size = p; }
>>      }
>>
>>      Rect r;
>>      r.origin = Point(1, 2);
>>      r.size = Point(5, 5);
>>      doubleMyValue(r.size.x);
>>      assert(r.lengths.x == 10); // fail
>>
>> How would your proposal affect the above code?
> 
> That code would still compile because the draft DIP does not require a 
> DotExpression a.b.c to have lvalues at all levels (i.e. all of a, b, and 
> c); as long as c is a ref, a and b don't matter.
> 
> This is hardly a problem with the DIP though because this compiles and 
> runs with the same uselessness:
> 
> r.size.x *= 2;
> 
> I could change the DIP to require lvalues throughout. Also, we should 
> probably change the language to disallow other constructs as well. 
> Essentially I think a.b.c should be an rvalue (even if it's ostensibly 
> an lvalue) if any of a, b, or c is an rvalue.

No, please don't. Rvalues can return lvalues just fine:

struct S
{
    int* x;
    ref int getX() { return *x; }
}

Note that the point being made was not to point out a problem, but to 
point out that rvalue references have existed for years (ever since 
struct member functions were added), and have not caused catastrophic 
failure.

-Steve


More information about the Digitalmars-d mailing list