Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::cout << x << " " << x++ << std::endl

Reply
Thread Tools

std::cout << x << " " << x++ << std::endl

 
 
Filipe Sousa
Guest
Posts: n/a
 
      02-05-2006
Hi!

Could someone explain to me why this operation is not what I was expecting?

int main()
{
int x = 2;
std::cout << x << " " << x++ << std::endl;
return 0;
}

main.cc: In function 'int main()':
warning: operation on 'x' may be undefined

The result is
3 2

I would expect 2 2.

Thanks
Filipe Sousa
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-05-2006
Filipe Sousa wrote:

> Hi!
>
> Could someone explain to me why this operation is not what I was
> expecting?
>
> int main()
> {
> int x = 2;
> std::cout << x << " " << x++ << std::endl;


This is equivalent to:

operator<< (
operator<< (
operator<< (
operator<< ( std::cout, x ),
" "
),
x++
),
std::endl;
);

In such an expression, the order of evaluation of subexpressions is
implementation defined. In particular, there is no guarantee that

operator<<( std::cout, x )

will be evaluated before

x++



> return 0;
> }
>
> main.cc: In function 'int main()':
> warning: operation on 'x' may be undefined
>
> The result is
> 3 2


That is just one possible result.

> I would expect 2 2.


That is another possible result.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
n2xssvv g02gfr12930
Guest
Posts: n/a
 
      02-05-2006
Filipe Sousa wrote:
> Hi!
>
> Could someone explain to me why this operation is not what I was expecting?
>
> int main()
> {
> int x = 2;
> std::cout << x << " " << x++ << std::endl;
> return 0;
> }
>
> main.cc: In function 'int main()':
> warning: operation on 'x' may be undefined
>
> The result is
> 3 2
>
> I would expect 2 2.
>
> Thanks
> Filipe Sousa


The result is undefined for your example. This is because x++ could
compiled to increment after the whole statement or immediately after use
as in this case. In short if you want predictable behaviour when using
operator ++ then the variable it is applied to should only appear once
in the expression being evaluated.

JB
 
Reply With Quote
 
TB
Guest
Posts: n/a
 
      02-05-2006
Filipe Sousa sade:
> Hi!
>
> Could someone explain to me why this operation is not what I was expecting?
>
> int main()
> {
> int x = 2;
> std::cout << x << " " << x++ << std::endl;
> return 0;
> }
>
> main.cc: In function 'int main()':
> warning: operation on 'x' may be undefined
>
> The result is
> 3 2
>
> I would expect 2 2.
>
> Thanks
> Filipe Sousa


http://www.parashift.com/c++-faq-lit...html#faq-39.16

--
TB @ SWEDEN
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-05-2006
TB wrote:

> Filipe Sousa sade:
>> Hi!
>>
>> Could someone explain to me why this operation is not what I was
>> expecting?
>>
>> int main()
>> {
>> int x = 2;
>> std::cout << x << " " << x++ << std::endl;
>> return 0;
>> }
>>
>> main.cc: In function 'int main()':
>> warning: operation on 'x' may be undefined
>>
>> The result is
>> 3 2
>>
>> I would expect 2 2.
>>
>> Thanks
>> Filipe Sousa

>
> http://www.parashift.com/c++-faq-lit...html#faq-39.16
>


I think, this is *not* a matter of sequence points. The expression is
equivalent to:

operator<< (
operator<< (
operator<< (
operator<< ( std::cout, x ),
" "
),
x++
),
std::endl;
);

and the various function calls provide sufficiently many sequence points.

The results are implementation defined because the evaluation order of the
parameters passed in a function call is implementation defined.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-05-2006
On Sun, 05 Feb 2006 18:27:38 +0000, n2xssvv g02gfr12930 wrote:

> Filipe Sousa wrote:
>>
>> Could someone explain to me why this operation is not what I was
>> expecting?
>>
>> int main()
>> {
>> int x = 2;
>> std::cout << x << " " << x++ << std::endl; return 0;
>> }

>
> The result is undefined for your example. This is because x++ could
> compiled to increment after the whole statement or immediately after use
> as in this case. In short if you want predictable behaviour when using
> operator ++ then the variable it is applied to should only appear once in
> the expression being evaluated.


TB's reference to sequence points[1] sits better as a reply to this
remark because, while this is excellent advice for beginners, it is not
entirely true due to expressions like x++ || x++ which have, I think,
predictable behaviour (however daft they may be in practice).

[1]
http://www.parashift.com/c++-faq-lit...html#faq-39.16

--
Ben.

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      02-05-2006
On Sun, 05 Feb 2006 19:23:40 +0000, Ben Bacarisse
<(E-Mail Removed)> wrote in comp.lang.c++:

> On Sun, 05 Feb 2006 18:27:38 +0000, n2xssvv g02gfr12930 wrote:
>
> > Filipe Sousa wrote:
> >>
> >> Could someone explain to me why this operation is not what I was
> >> expecting?
> >>
> >> int main()
> >> {
> >> int x = 2;
> >> std::cout << x << " " << x++ << std::endl; return 0;
> >> }

> >
> > The result is undefined for your example. This is because x++ could
> > compiled to increment after the whole statement or immediately after use
> > as in this case. In short if you want predictable behaviour when using
> > operator ++ then the variable it is applied to should only appear once in
> > the expression being evaluated.

>
> TB's reference to sequence points[1] sits better as a reply to this
> remark because, while this is excellent advice for beginners, it is not
> entirely true due to expressions like x++ || x++ which have, I think,
> predictable behaviour (however daft they may be in practice).


That still has to do with sequence points. The logical operators ||
and && do provide sequence points, so the behavior is well defined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      02-05-2006
On Sun, 05 Feb 2006 13:53:06 -0500, Kai-Uwe Bux <(E-Mail Removed)>
wrote in comp.lang.c++:

> TB wrote:
>
> > Filipe Sousa sade:
> >> Hi!
> >>
> >> Could someone explain to me why this operation is not what I was
> >> expecting?
> >>
> >> int main()
> >> {
> >> int x = 2;
> >> std::cout << x << " " << x++ << std::endl;
> >> return 0;
> >> }
> >>
> >> main.cc: In function 'int main()':
> >> warning: operation on 'x' may be undefined
> >>
> >> The result is
> >> 3 2
> >>
> >> I would expect 2 2.
> >>
> >> Thanks
> >> Filipe Sousa

> >
> > http://www.parashift.com/c++-faq-lit...html#faq-39.16
> >

>
> I think, this is *not* a matter of sequence points. The expression is
> equivalent to:
>
> operator<< (
> operator<< (
> operator<< (
> operator<< ( std::cout, x ),
> " "
> ),
> x++
> ),
> std::endl;
> );
>
> and the various function calls provide sufficiently many sequence points.
>
> The results are implementation defined because the evaluation order of the
> parameters passed in a function call is implementation defined.


It really still is sequence points. There is no guarantee in the
original expression, or in the expression as you have rewritten it,
that any of the sequence points occurs in between the two accesses of
'x'. The results are undefined for that reason.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-06-2006
Jack Klein wrote:

> On Sun, 05 Feb 2006 13:53:06 -0500, Kai-Uwe Bux <(E-Mail Removed)>
> wrote in comp.lang.c++:
>

[snip]
>> I think, this is *not* a matter of sequence points. The expression is
>> equivalent to:
>>
>> operator<< (
>> operator<< (
>> operator<< (
>> operator<< ( std::cout, x ),
>> " "
>> ),
>> x++
>> ),
>> std::endl;
>> );
>>
>> and the various function calls provide sufficiently many sequence points.
>>
>> The results are implementation defined because the evaluation order of
>> the parameters passed in a function call is implementation defined.

>
> It really still is sequence points. There is no guarantee in the
> original expression, or in the expression as you have rewritten it,
> that any of the sequence points occurs in between the two accesses of
> 'x'. The results are undefined for that reason.


Thanks for the correction,

I think, I see what you mean. Just to check my understanding: the following
is implementation defined behavior, but not undefined


int post_inc( int & x ) {
int dummy = x;
++x;
return( dummy );
}

operator<< (
operator<< (
operator<< (
operator<< ( std::cout, x ),
" "
),
post_inc( x )
),
std::endl;
);

The reason would be that in the evaluation of sub-expressions one of the
calls operator<<( std::cout, x ) and post_inc( x ) comes first.
Whichever comes first has a sequence point upon return, which separates the
two accesses to x. The reason this does not apply to the original
expression is that x++ is not a function call. Is that correct?


Thanks again

Kai-Uwe Bux
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-06-2006
On Sun, 05 Feb 2006 17:48:12 -0600, Jack Klein wrote:

> On Sun, 05 Feb 2006 19:23:40 +0000, Ben Bacarisse <(E-Mail Removed)>
> wrote in comp.lang.c++:
>
>> On Sun, 05 Feb 2006 18:27:38 +0000, n2xssvv g02gfr12930 wrote:
>>
>> > In short if you want predictable behaviour when
>> > using operator ++ then the variable it is applied to should only
>> > appear once in the expression being evaluated.

>>
>> TB's reference to sequence points[1] sits better as a reply to this
>> remark because, while this is excellent advice for beginners, it is not
>> entirely true due to expressions like x++ || x++ which have, I think,
>> predictable behaviour (however daft they may be in practice).

>
> That still has to do with sequence points. The logical operators || and
> && do provide sequence points, so the behavior is well defined.


I thought that is what I had said :/

--
Ben.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments