Connecting Tech Pros Worldwide Forums | Help | Site Map

Sequence points.

kwrzalik@gmail.com
Guest
 
Posts: n/a
#1: May 26 '06
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.


benben
Guest
 
Posts: n/a
#2: May 26 '06

re: Sequence points.


kwrzalik@gmail.com wrote:[color=blue]
> 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.
>[/color]

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

Ben
Jonathan Mcdougall
Guest
 
Posts: n/a
#3: May 26 '06

re: Sequence points.


kwrza...@gmail.com wrote:[color=blue]
> 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.[/color]

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

kwrzalik@gmail.com
Guest
 
Posts: n/a
#4: May 26 '06

re: Sequence points.


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.

kwrzalik@gmail.com
Guest
 
Posts: n/a
#5: May 26 '06

re: Sequence points.


Are you sure it's the end of the function and not the so called
sequence point?

Rolf Magnus
Guest
 
Posts: n/a
#6: May 26 '06

re: Sequence points.


kwrzalik@gmail.com wrote:
[color=blue]
> 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.[/color]

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.

benben
Guest
 
Posts: n/a
#7: May 26 '06

re: Sequence points.


kwrzalik@gmail.com wrote:[color=blue]
> Are you sure it's the end of the function and not the so called
> sequence point?
>[/color]

Negative...I'll go back and read the standard...

Regards,
Ben
Kyku
Guest
 
Posts: n/a
#8: May 26 '06

re: Sequence points.


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.

Closed Thread