Re: c++ unary increment operator
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 11/01/03
- Next message: Jacek Dziedzic: "Are std::strings guaranteed to default-initalize to ""?"
- Previous message: GBartgo: "gaussian mixture?"
- In reply to: MSDousti: "c++ unary increment operator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 01 Nov 2003 19:52:55 GMT
"MSDousti" <MSDousti@myrealbox.com> wrote in message
news:bfefd8f8.0311011026.edfd3a2@posting.google.com...
> Hi,
> I'm confused with C++'s unary increment operator(++).
> Consider the following code:
>
> //////////////////////////////////////////////////////////////////////
> #include <iostream>
> using namespace std;
>
> int main()
> {
> int a=2;
> a= (a==2 ? a+=100:0) + (a>3 ? a+=200:1000) + a++;
> cout << a << endl;
> return 0;
> }
> //////////////////////////////////////////////////////////////////////
>
> I have run this code both on linux (using g++) and windows (VS.NET).
> Surprisingly, the resaults were incompatible!
> g++ answers: 507
> VS answers: 707
The language does not specify the order in which the
expressions:
(a==2 ? a+=100:0)
(a>3 ? a+=200:1000)
a++
... are evaluated.
> Can anyone help?
Break the code down to where you have sufficient control
over evaluation order. Look up 'sequence points'. I can't
tell you what you need here, since you don't say which result
you want.
-Mike
- Next message: Jacek Dziedzic: "Are std::strings guaranteed to default-initalize to ""?"
- Previous message: GBartgo: "gaussian mixture?"
- In reply to: MSDousti: "c++ unary increment operator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]