Order of evaluation of arguments using std::cout
The evaluation order of elements in an expression is unspecified (except some very particular cases, such as the &&
and ||
operators and the ternary operator, which introduce sequence points); so, it's not guaranteed that test
will be evaluated before or after foo(test)
(which modifies it).
If your code relies on a particular order of evaluation the simplest method to obtain it is to split your expression in several separated statements.
The answer to this question changed in C++17.
Evaluation of overloaded operators are now sequenced in the same way as for built-in operators (C++17 [over.match.oper]/2).
Furthermore, the <<
, >>
and subscripting operators now have the left operand sequenced before the right, and the postfix-expression of a function call is sequenced before evaluation of the arguments.
(The other binary operators retain their previous sequencing, e.g. +
is still unsequenced).
So the code in the question must now output Value of test is : 0 Return value of function is : 1 Value of test : 1
. But the advice "Don't do this" is still reasonable, given that it will take some time for everybody to update to C++17.
Order of evaluation is unspecified. It is not left-to-right, right-to-left, or anything else.
Don't do this.