cout question

C

coder

I'm new to C++ and currently I'm trying to make my own stack.

This is my main() function:

int main()
{
Stack<int> iStack;

iStack.push(10);
iStack.push(13);
iStack.push(12);

cout << iStack.pop() << endl;
cout << iStack.pop() << endl;
cout << iStack.pop() << endl;

return 0;
}

When I run the program, I get the output as:
12
13
10

which indicates that the program is working OK.


However, when I change the 3 "popping" lines to the following:

cout << iStack.pop() << endl << iStack.pop() << endl << iStack.pop()
<< endl;

the output gets reversed:
10
13
12

Could anyone explain why is that so?
 
S

Spidey07

I'm new to C++ and currently I'm trying to make my own stack.

This is my main() function:

int main()
{
Stack<int> iStack;

iStack.push(10);
iStack.push(13);
iStack.push(12);

cout << iStack.pop() << endl;
cout << iStack.pop() << endl;
cout << iStack.pop() << endl;

return 0;

}

When I run the program, I get the output as:
12
13
10

which indicates that the program is working OK.

However, when I change the 3 "popping" lines to the following:

cout << iStack.pop() << endl << iStack.pop() << endl << iStack.pop()
<< endl;

the output gets reversed:
10
13
12

Could anyone explain why is that so?

its abvious from the output itself
the order of evaluation of cout
 
O

osmium

coder said:
I'm new to C++ and currently I'm trying to make my own stack.

This is my main() function:

int main()
{
Stack<int> iStack;

iStack.push(10);
iStack.push(13);
iStack.push(12);

cout << iStack.pop() << endl;
cout << iStack.pop() << endl;
cout << iStack.pop() << endl;

return 0;
}

When I run the program, I get the output as:
12
13
10

which indicates that the program is working OK.


However, when I change the 3 "popping" lines to the following:

cout << iStack.pop() << endl << iStack.pop() << endl << iStack.pop()
<< endl;

the output gets reversed:
10
13
12

Could anyone explain why is that so?

The order of evaluation of function arguments is not specified by the
language. Your compiler chose this order in this particular instance.
Moral: If you care, use parens, or rejigger what you wrote.
 
P

Philip Potter

osmium said:
The order of evaluation of function arguments is not specified by the
language. Your compiler chose this order in this particular instance.
Moral: If you care, use parens, or rejigger what you wrote.

Parens won't help here. The problem is not order of precedence but order
of evaluation.

To the OP: the calls to iStack.pop() do not necessarily occur
left-to-right on the line. What your compiler has done is this: it
called the rightmost pop() first, followed by the middle, then the
leftmost. It then outputs the result from the leftmost pop(), which
executed last and returned 10. Then it returns the middle pop(), which
returned 13, and the rightmost pop(), which executed first and returned 12.

However, if you compiled this under a different compiler, the order of
evaluation might be left->middle->right or even middle->right->left. A
C++ compiler is allowed to choose any order it likes.

It is not allowed to do this in your first version because the
semicolons act as "sequence points": they cause all previous expressions
to be completely evaluated before proceeding. Therefore they constrain
the order in which the functions can be called.
 
C

coder

Parens won't help here. The problem is not order of precedence but order
of evaluation.

To the OP: the calls to iStack.pop() do not necessarily occur
left-to-right on the line. What your compiler has done is this: it
called the rightmost pop() first, followed by the middle, then the
leftmost. It then outputs the result from the leftmost pop(), which
executed last and returned 10. Then it returns the middle pop(), which
returned 13, and the rightmost pop(), which executed first and returned 12.

However, if you compiled this under a different compiler, the order of
evaluation might be left->middle->right or even middle->right->left. A
C++ compiler is allowed to choose any order it likes.

It is not allowed to do this in your first version because the
semicolons act as "sequence points": they cause all previous expressions
to be completely evaluated before proceeding. Therefore they constrain
the order in which the functions can be called.


Whew, thanks, Philip. That's cleared it up for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top