Issue 1974 - What's your opinion?

Atila Neves atila.neves at gmail.com
Mon Oct 28 16:53:06 UTC 2019


On Saturday, 26 October 2019 at 15:30:13 UTC, kinke wrote:
> On Saturday, 26 October 2019 at 14:41:38 UTC, John Colvin wrote:
>> I think the lowering is a distraction.
>>
>> += on an rvalue is invalid, so the lowering should never 
>> happen.
>>
>> The implementation of operator overloads may be done with 
>> regular functions, but the "interface" of += doesn't make 
>> sense on an rvalue. If you want to call your opOpAssign on an 
>> rvalue, that's fine, but that's not the same as +=
>
> Exactly what I'm thinking. Implementation-wise, it's probably 
> not that hard to perform the lhs-lvalue check before lowering.
>
> C++ btw allows rvalues: https://godbolt.org/z/qZ3qrj

Moreover, C++ can overload on the "rvalueness" of this, thereby 
allowing the user to disallow calling operator+= on rvalues:

---------
struct S {
     int i = 0;
     // notice the `&` between `(int)` and the opening curly
     // this means that `this` can only be bound to lvalues
     S& operator+=(int) & { return *this; }
};


int main() {
     S s{};
     s += 42;   // fine, lvalue
     S() += 3;  // won't compile
}
---------


More information about the Digitalmars-d mailing list