PROPOSAL: Operator overloading by static member function

Nathan Reed nathaniel.reed at gmail.com
Sat Oct 13 19:21:15 PDT 2007


oldrev wrote:
> Daniel Keep Wrote:
> 
>>
>> oldrev wrote:
>>> It's a simple way to avoid using the ugly "opAdd_r", eg:
>>>
>>> struct Currency
>>> {
>>>   private int m_value;
>>>
>>>   public static Currency opAdd(Currency lhs, Currency rhs) {
>>>     return Currency(lhs.m_value + rhs.m_value);
>>>   }
>>> }
>> And how are you going to overload static opAdd_r, then?
>>
>> I really don't see any need for this; opAdd_r is perfectly fine.
>>
>> 	-- Daniel
> 
> We do not need opAdd_r anymore, it will be overloaded by static opAdd.
> 
> static A opAdd(A a, int i);
> static A opAdd(int i, A a);
> 
> a+100 // it equals to opAdd(a, 100);
> 100+a //opAdd(100, a)
> 
> Regards,
> - oldrev

I like the idea of a non-member function for overloading operators, 
since the definition looks more symmetric and avoids giving undue 
'emphasis' to one of the values involved.  On the other hand, this would 
be a breaking change and I'm not sure it's worth it.

Also, one advantage of the current way of doing overloaded operators is 
that it enforces that operators that are supposed to be commutative, 
like +, can't be redefined to be noncommutative.

Thanks,
Nathan Reed



More information about the Digitalmars-d mailing list