cout print order?

I have a function that I am passing two arguments, one of them by value and one by reference.

int problem_001(int,int& t);

Before calling "problem_001", "t" has a known value (let's say 0).
Inside "problem_001", I assign "t" a new value (let's say 10).

In my main program, I have this statement:

cout << "Problem 01 = " << problem_001(1000,t) << " , " << t <<endl;

In this case "t" still prints with the original value of "0".

but, if I change my statement to:

cout << "Problem 01 = " << problem_001(1000,t);
cout << " , " << t <<endl;

"t" now prints the value I expect i.e. "10".

Is there a explanation for this please.

Thanks
Rob
According to the C++ Standard "
The order of evaluation of function arguments is unspecified.
"
So the first statement has an undefined behavior. According to C conventions usually arguments are evaluated from right to left but you should not rely upon that.
In the next statements there is no such problem because statements are executed sequentially.
Last edited on
@OP - You may find http://stackoverflow.com/questions/10778627/why-is-i-i-1-undefined-behavior-in-c11 interesting reading. The principals involved are the same.
Topic archived. No new replies allowed.