In C++, is A+=B preferable to A=A+B in the same way ++A is to A++?
I understand that a "++A" preincrement is guaranteed to be at least as fast as a "A++" postincrement. This is discussed many places including here and here. Likewise, A+=B is expected to be at least as fast as A=A+B, as here.
I was looking at this for ++:
//From https://herbsutter.com/2013/05/13/gotw-2-solution-temporary-objects/
T T::operator++(int)() {
auto old = *this; // remember our original value
++*this; // always implement postincr in terms of preincr
return old; // return our original value
}
My reasoning is that in the worst case (probably from complex object types) A=A+B would have to retrieve and store A and add in B before saving it back to the original A location, while A+=B would take B and add it to A directly. Is my reasoning correct?
It is expected that basic types are rearranged as the same at compile time, and that this really only applies to complex objects requiring operator overloading.
Does this extend generically to most imperative languages?