Sequence points.

K

kwrzalik

Hello,
I was solving a C++ quiz that has the following question:


What is the outcome of running the following code:

int c=0;
cout<<c++<<c;

The answer says undefined. But in this case, isn't this statement
translated into:

cout.operator<<(c++).operator<<(c);

There should be a sequnce point before calling a funcion after
evaluation of the arguments. So shouldn't the incremented value of c be
stored before the call to first <<. If so it should be available for
the second <<, and the result would be 01.

Please help me with this confusion.
 
B

benben

Hello,
I was solving a C++ quiz that has the following question:


What is the outcome of running the following code:

int c=0;
cout<<c++<<c;

The answer says undefined. But in this case, isn't this statement
translated into:

cout.operator<<(c++).operator<<(c);

There should be a sequnce point before calling a funcion after
evaluation of the arguments. So shouldn't the incremented value of c be
stored before the call to first <<. If so it should be available for
the second <<, and the result would be 01.

Please help me with this confusion.

Compilers have right to delay the increment operation on c until the end
of the statement.

Ben
 
J

Jonathan Mcdougall

Hello,
I was solving a C++ quiz that has the following question:


What is the outcome of running the following code:

int c=0;
cout<<c++<<c;

The answer says undefined. But in this case, isn't this statement
translated into:

cout.operator<<(c++).operator<<(c);

There should be a sequnce point before calling a funcion after
evaluation of the arguments. So shouldn't the incremented value of c be
stored before the call to first <<. If so it should be available for
the second <<, and the result would be 01.

There is a sequence point between the function calls, but not between
the evaluation of the arguments. That means, whether "c" is evaluated
before or after "c++" is undefined. Don't do that.


Jonathan
 
K

kwrzalik

Could you please elaborate on this more verbosely? I understand that if
c++ is evaluated as an argument of a function and there's a sequence
point bofore calling this function the increment operator must be
executed and saved at this point. Because the dot operator bind from
left to right, the c should be incremented and saved so that it is
accessible in the second operarator<< call.
 
K

Kyku

You seem to be right. Thank you for your explanation. I didn't think
about it that the two c's can be evaluated at any point.
 
R

Rolf Magnus

Could you please elaborate on this more verbosely? I understand that if
c++ is evaluated as an argument of a function and there's a sequence
point bofore calling this function the increment operator must be
executed and saved at this point.

Yes, but the other one can be executed already, too. It doesn't have to be
deferred to after the first function (or even after operator++) is called.
The compiler is free to first evaluate c, then c++, then call the first
operator<<, then the second one.
At least that's how I understand it.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top