Operator overloading -- lets collect some use cases

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Dec 30 08:38:22 PST 2008


Don wrote:
> Frits van Bommel wrote:
>> Don wrote:
>>> A straightforward first step would be to state in the spec that "the 
>>> compiler is entitled to assume that X+=Y yields the same result as 
>>> X=X+Y"
>>
>> That doesn't hold for reference types, does it?
> 
> I thought it does? Got any counter examples?

For any class type, with += modifying the object and + returning a new one:

B1 = A;
B1 += C; // A is modified, B1 stays same reference
assert(A is B1);

B2 = A;
B2 = B2 + C; // Doesn't touch A, B2 is reassigned a different ref
assert(A !is B2);

You can't just arbitrarily substitute between these two.
Certainly you can't substitute + with +=.
I suppose substituting += with + might be reasonable if no += is provided.



More information about the Digitalmars-d mailing list