Operator overloading -- lets collect some use cases

Christopher Wright dhasenan at gmail.com
Tue Dec 30 06:35:31 PST 2008


Don wrote:
> Some observations based on the use cases to date:
> (1)
> a += b is ALWAYS a = a + b (and likewise for all other operations).
> opXXXAssign therefore seems to be a (limited) performance optimisation. 
> The compiler should be allowed to synthesize += from +. This would 
> almost halve the minimum number of repetitive functions required.

Not quite true:
class A
{
	int value;
	A opAdd(A other) { return new A(value + other.value); }
	A opAddAssign(A other) { value += other.value; }
}

class B
{
	A a;
	this (A value) { a = value; }
}

void main ()
{
	auto a = new A;
	auto b1 = new B(a);
	auto b2 = new B(a);
	auto a2 = new A;
	b1.a += a2; // okay, b1.a is b2.a
	b1.a = b1.a + a2; // now b1.a !is b2.a
}



More information about the Digitalmars-d mailing list