Connecting Tech Pros Worldwide Help | Site Map

Sequence points.

  #1  
Old May 26th, 2006, 09:15 AM
kwrzalik@gmail.com
Guest
 
Posts: n/a
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.

  #2  
Old May 26th, 2006, 10:03 AM
benben
Guest
 
Posts: n/a

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
  #3  
Old May 26th, 2006, 10:03 AM
Jonathan Mcdougall
Guest
 
Posts: n/a

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

  #4  
Old May 26th, 2006, 10:45 AM
kwrzalik@gmail.com
Guest
 
Posts: n/a

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.

  #5  
Old May 26th, 2006, 10:45 AM
kwrzalik@gmail.com
Guest
 
Posts: n/a

re: Sequence points.


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

  #6  
Old May 26th, 2006, 11:15 AM
Rolf Magnus
Guest
 
Posts: n/a

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.

  #7  
Old May 26th, 2006, 12:15 PM
benben
Guest
 
Posts: n/a

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
  #8  
Old May 26th, 2006, 01:25 PM
Kyku
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
sequence points Jrdman answers 7 August 17th, 2008 08:15 PM
Purpose of sequence points Daniel Kraft answers 4 June 16th, 2007 12:05 PM
What are the sequence points in a full expression? lovecreatesbea...@gmail.com answers 1 October 9th, 2006 04:05 PM
Knowing your sequence points akarl answers 7 November 15th, 2005 01:46 AM
Does C++ have sequence points ? Timothy Madden answers 4 July 22nd, 2005 08:17 PM