alex said:
BlankThis maybe a known issue, but I stepped on it just now.
Basically it boils down to: in C#
int x = 0;
Console.Write("{0}", x++ + x++);
gives 1, when in C/C++
int x = 0;
printf("%d\n", x++ + x++);
gives 0, which I think is correct.
What goes the group think about it?
It's only "correct" if you consider your C++ implementation to be
authorative. In the standards, that particular expression is undefined
because the variables have multiple side-effects and it's up to the
compiler to arrange the evaluation for optimum performance. This means
that a different C++ compiler, or even the same brand/version of the C++
compiler on a different platform, might produce a different result.
The C# standards only say that the actual operation happens after the
value has been taken, as outlined in topic 7.5.9 of the C# language
specs. It appears here that the entire expression is evaluated before
the variable is incremented. I wouldn't be surprised if somewhere it
said that double side-effects produce undefined results.
What I think of it ? I think that it's a contrived example and any
issues you have with it should be left alone.
In any case, it isn't going to change so the best idea is just to
remember that this is not something that you do and then it's not an
issue any more.
In C++, with macros, this is a more common issue than in C# where macros
are not available.