cout << string_1 << string_2 << string_3;
The line above is processed from left to right, first the operator "<<" operates on "cout" and "string_1", which returns an ostream object which is later used to operate on "string_2", and so on to "string_3".
a = b = 5;
In the line above, the code is processed from right to left. First the operator "=" operates on "b" and "5", which returns an int
object to operate with "a" on the next "=" operator.
I might be wrong on how these lines are processed.
Please help me understand why the compiler is changing the order of operations in both cases.
CodePudding user response:
Because that's how the operator associativity is defined: https://en.cppreference.com/w/cpp/language/operator_precedence
– UnholySheep
CodePudding user response:
This is a wise design decision.
Because you intuitively expect
cout<< string_1 << string_2 << string_3;
to emit the three strings in that order, and you expect
a = b = 5;
to assign 5
to both a
and b
(rather than performing a= b; b= 5;
)