The title is be misleading, so please read entire question :-).
By "compound assignment operator" I have in mind a construct like this op=
, for example +=
. Pure assignment operator (=
) does not belong to my question.
By "why" I don't mean an opinion, but resource (book, article, etc) when some of the designers, or their coworkers, etc. express their reasoning (i.e. the source of the design choice).
I am puzzled by asymmetry found in C++ and C# (yes, I know C# is not C++ 2.0) -- in C++ you would overload operator +=
and then almost automatically write appropriate +
operator relying on previously defined operator. In C# it is in reverse -- you overload +
and +=
is synthesised for you.
If I am not mistaken the later approach kills a chance for optimization in case of actual +=
, because new object has to be created. So there has to be some big advantage of such approach, but MSDN is too shy to tell about it.
And I wonder what that advantage is -- so if you spotted explanation in some C# book, tech-talk video, blog entry, I will be grateful for the reference.
The closest thing I found is a comment on Eric Lippert blog Why are overloaded operators always static in C#? by Tom Brown. If the static overloading was decided first it simply dictates which operators can be overloaded for structs. This further dictates what can be overloaded for classes.
+=
first seems absurd; why would you overload a combined operation rather than the parts of it?*=
mutating a reference type to be semantically incorrect.