473,738 Members | 10,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Feb 5 '06 #1
12 6106
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
Feb 5 '06 #2
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
Feb 5 '06 #3
TB
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
Feb 5 '06 #4
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
Feb 5 '06 #5
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.

Feb 5 '06 #6
On Sun, 05 Feb 2006 19:23:40 +0000, Ben Bacarisse
<be********@bsb .me.uk> 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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 6 '06 #7
On Sun, 05 Feb 2006 13:53:06 -0500, Kai-Uwe Bux <jk********@gmx .net>
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 6 '06 #8
Jack Klein wrote:
On Sun, 05 Feb 2006 13:53:06 -0500, Kai-Uwe Bux <jk********@gmx .net>
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
Feb 6 '06 #9
On Sun, 05 Feb 2006 17:48:12 -0600, Jack Klein wrote:
On Sun, 05 Feb 2006 19:23:40 +0000, Ben Bacarisse <be********@bsb .me.uk>
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.

Feb 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9031
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex' undeclared (first use this function)
3
2476
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going wrong? I am using g++ (GCC) 3.3.5 on a Debian Sarge system. The compiler complains: //**************************** //**************************** Compiler output starts *********** cd /home/red/tmp/testprogs/
1
1514
by: bluekite2000 | last post by:
I have #include<iostream> #include<complex> typedef std::complex<float> ComplexSingle; int main(void) { ComplexSingle a(2,3); std::cout<<a.real()<<std::endl;//this works std::cout<<a.imag()<<std::endl;//this works
3
2110
by: silverburgh.meryl | last post by:
I have this linker error, and I would need some help in resolving it: I have put "#include <iostream>" in my .cpp. I am not sure why I can't link. It compiles fine. /usr/bin/ld: .../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)@@GLIBCXX_3.4'
7
4693
by: imutate | last post by:
How do I implement << ala std::cout for vector template ? I already have the following: #include <vector> template < typename T > class Vec : public std::vector< T { public: Vec() { } Vec( int s ) : std::vector<T>(s) { }
46
3390
by: suresh | last post by:
Hi, For this code snippet, I get the following output, which I am unable to understand. (2^31 = 2147483648) cout<< -2147483648 << endl; cout << numeric_limits<int>::min() <<',' << numeric_limits<int>::max()<< endl;
5
2462
by: Alex Vinokur | last post by:
void foo (int n) { std::ostringstream oss; oss << "ABCD: " << n << std::endl; std::cout << oss.str() << std::flush; } That function has been invoked in multiprocessing mode.
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.