Why does cout << not print inputs from left to right?
Asked Answered
Q

3

30

the following code:

myQueue.enqueue('a');
myQueue.enqueue('b');
cout << myQueue.dequeue() << myQueue.dequeue();

prints "ba" to the console

while:

myQueue.enqueue('a');
myQueue.enqueue('b');
cout << myQueue.dequeue();
cout << myQueue.dequeue();

prints "ab" why is this?

It seems as though cout is calling the outermost (closest to the ;) function first and working its way in, is that the way it behaves?

Quesada answered 24/1, 2010 at 22:44 Comment(0)
L
32

There's no sequence point with the << operator so the compiler is free to evaluate either dequeue function first. What is guaranteed is that the result of the second dequeue call (in the order in which it appears in the expression and not necessarily the order in which it is evaluated) is <<'ed to the result of <<'ing the first (if you get what I'm saying).

So the compiler is free to translate your code into some thing like any of these (pseudo intermediate c++). This isn't intended to be an exhaustive list.

auto tmp2 = myQueue.dequeue();
auto tmp1 = myQueue.dequeue();
std::ostream& tmp3 = cout << tmp1;
tmp3 << tmp2;

or

auto tmp1 = myQueue.dequeue();
auto tmp2 = myQueue.dequeue();
std::ostream& tmp3 = cout << tmp1;
tmp3 << tmp2;

or

auto tmp1 = myQueue.dequeue();
std::ostream& tmp3 = cout << tmp1;
auto tmp2 = myQueue.dequeue();
tmp3 << tmp2;

Here's what the temporaries correspond to in the original expression.

cout << myQueue.dequeue() << myQueue.dequeue();
|       |               |    |               |
|       |____ tmp1 _____|    |_____ tmp2 ____|
|                       |
|________ tmp3 _________|
Livorno answered 24/1, 2010 at 22:48 Comment(6)
so, in the top example, would std::ostream& tmp3 = cout << tmp1; tmp3 << tmp2; be like saying "cout << tmp1 << tmp2;"? Or something I'm missing?Quesada
@segfault: Yes, because that is the way << associates in C++ grammar. a << b << c always groups as (a << b) << c.Livorno
but by that logic, wouldnt saying cout << a << b be saying (cout << a) << b and do anything necessary to cout a first (i.e. call myQueue.dequeue())?Quesada
The associativity doesn't say anything about the order in which the subexpressions are evaluated. cout << a << b always means (cout << a) << b but the compiler is free to evaluate b first, then a, then the first << operation and the the second. This is exactly the point that I made in my answer.Livorno
Sorry to comment on an old post, but C++11 1.9/15 (C++03 1.9/17) seems to imply that the sequencing constraints on overloaded operators are different. If my understanding is correct, the above is either cout.operator<<(a).operator<<(b), or operator<<(operator<<(cout,a),b) depending on whether the operator is a member or free function. Both would imply a defined evaluation order for subexpressions a and b. Is that correct?Janot
@jogojapan: This particular fact hasn't changed in C++11 although the concept of sequence point has been replaced. In neither of the expressions that you've shown is there a constraint between the evaluation of a and b. Either may be evaluated before the other. There are other constraints, such as b must be evaluated before either operator<< as it is a direct or indirect argument to both of them and a must be evaluated before the operator<< to which it is a direct argument but the expressions cout, a, and b may be evaluated in any order w.r.t. each other.Livorno
W
9

The call from your example:

cout << myQueue.dequeue() << myQueue.dequeue();

translates to the following expression with two calls of operator<< function:

operator<<( operator<<( cout, myQueue.dequeue() ), myQueue.dequeue() );
-------------------- 1
---------2

The order of evaluation of cout, myQueue.dequeue() is unspecified. However, the order of operator<< function calls is well specified, as marked with 1 and 2

Wandis answered 24/1, 2010 at 23:41 Comment(0)
S
8

Since C++17 the behaviour of this code has changed; the left operand of << is sequenced before the right operand of <<, even when it is an overloaded operator. The output must now be ab.

For further reading see: What are the evaluation order guarantees introduced by C++17?.

Socorrosocotra answered 27/6, 2018 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.