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?
There’s no sequence point with the
<<operator so the compiler is free to evaluate eitherdequeuefunction first. What is guaranteed is that the result of the seconddequeuecall (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.
or
or
Here’s what the temporaries correspond to in the original expression.