c++ operator overloading += works but << doesn't work
This: {10, 20}
is a braced-init-list. It is not an expression. As such, it can appear only in specific pieces of C++ grammar.
For example, braced-init-lists can appear after a typename, which means they initialize a prvalue of that type. They can appear as an argument to a function. And (among several others) they can appear on the right-hand side of an assignment operator.
Note that +=
is an assignment operator.
<<
is not one of these specific places. Therefore, a naked braced-init-list cannot appear on either side of a <<
expression. This is regardless of the fact that the <<
expression will be converted into a call to operator<<
and thus the braced-init-list could be considered a function argument. C++ grammar simply doesn't allow a braced-init-list to appear there, so the compiler never gets far enough to even attempt overload resolution to figure out which function to call.