TITLE: Order of evaluation or is it grouping? PROBLEM: cs_phh@ug.cs.ust.hk (Pang Hung Hing Anthony) Is there any evaluation order in << output operator? From left to right, right to left or undef? RESPONSE: Paul J. Lucas

It's the same as the << shift operator. RESPONSE: Keith Pomakis ...which is evaluated from left to right. If it were evaluated any other way, the output of lines such as cout << "x = " << x << '\n'; would be unpredictable. RESPONSE: pete@genghis.interbase.borland.com (Pete Becker), 7 Nov 94 This is because the << operator groups left to right, not because it evaluates left to right. In fact, the order of evaluation is unspecified. Grouping refers to figuring out which parameters go with which operators. When an operator groups left to right, it means that in an expression like the one above, the left-most operator gets the two parameters on either side of it. In this case, the first << is applied to cout and to "x = ". The result of that operation is the left operand for the next << operator. Order of evaluation is completely separate. In the expression above it is irrelevant, since all the operands are constants. Here's a more complex example: cout << f() << g(); The grouping here is that the first << operator is applied to cout and the result of calling f(). The next << operator is applied to the result of the first one and to the result of calling g(). Just like above. The added complication here comes from the calls to f() and g(), that is, the evaluation of the operands of the << operators. As I mentioned above, this is unspecified, except that the operands of any function must be evaluated before calling the function. That means that the compiler is free to evaluate this expression in any of the following ways: evaluate f() evaluate first << evaluage g() evaluate second << evaluate f() evaluate g() evaluate first << evaluate second << evaluate g() evaluate f() evaluate first << evaluate second << In particular, note that the compiler can, and often does, call g() before calling f().