postincrement behaviour (differences between dmd and gdc)

Pragma ericanderton at yahoo.removeme.com
Wed Jan 24 12:53:11 PST 2007


Nicolai Waniek wrote:
> Hello again,
> 
> I have a huge arguing going on with a friend of mine, which behaviour of
> x++ is the right one. This has in fact to do with D, so read on :)
> 
> This is our example application:
> 
> int x = 5;
> x = x++;
> x = x++;
> print(x);
> 
> 
> I argue: the result printed is 7.
> He argues: the result printed is 5.
> 
> the "++" operator is defined as: "returns the value and then increments
> it by 1". so I guess, the execution should be as follows:
> 
> 
> int x = 5;
> -> x "gains" the value 5.
> 
> 
> x = x++;
> -> first, (x = x) is executed. afterwards, x is incremented by 1. as x
> and x are the same variables, x now has the value 6.
> 
> x = x++;
> -> the same as above, so x is now 7.
> 
> 
> Allright, now let's look what the compilers do:
> 
> C/C++ compiled with GCC:	7
> java compiled with SUN's:	5
> D compiled with DMD:		5
> D compiled with GDC:		7
> 
> Allright, so please explain this behaviour to me?

You forgot this:

 > C compiled with GDC:          5

So at least it's consistent D and C for each backend - this looks like a DMC vs GCC problem. :/

If you use a different test, you'll see that DMC/DMD treats the post-increment as truly following the evaluation of the 
incremented term, within the expression.

x = 5;
x = x++ + 5; // x is now 10

or

x = 5;
x = x++ + x; // x is now 11

-- 
- EricAnderton at yahoo



More information about the Digitalmars-d mailing list