addition for a user defined type was defined by operator+, but += does not resolve to it, why?
typedef std::array<std::uint64_t,4> myvec_t;
inline myvec_t operator+(myvec_t foo, myvec_t bar){return mod_add(foo,bar);}
myvec_t a,b;
a+=b; // g++ says: error: no viable overloaded '+='
EDIT: several people sufficiently answered the narrow question, I will accept an answer which explains why did the standard allow people to overload '+='(a,b) to do something different from a=a+b ?
+=
operator is a separate operator, it's not+
followed by assignment.+
) this canonical implementations reference says that one should implement the operator through the combined assignment operator. That is youroperator+
should useoperator+=
in its implementation.a += b
to do something different froma = a + b
? Then the standard doesn't really allow it, but it also doesn't forbid it. If you as the programmer want+=
do do something different from+
followed by=
then that's up to you. It's bad and will lead to confusion, but it's up to you. C++ doesn't hinder you in doing what you want, and if you want to shoot yourself in the foot then (again) it's up to you.+=
you can reuse existing memory instead of creating a new object and assigning it tothis
, while+
doesn't need to return myvec_t (i.e. string builder like pattern).