Re: Same code and different result, Why?
- From: "Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx>
- Date: Fri, 7 Nov 2008 16:27:33 +0200
"Tommy" wrote:
Just consider from a functional programming standpoint, where
IMV, many programmers trained as engineers do alot.
inline int INC(int &i) { return i++; }
// note: don't focus on the inline, just an illustration
..
int x = 36, y = 20;
x = INC(x) + INC(y);
the result is correctly different. Sure, who would do this when
the language itself offers this functionality.
But the whole point is that the language offers two distinct ways
to increment:
inline int POST_INC(int &i) { int tmp = i; i += 1; return tmp; }
inline int PRE_INC(int &i) { i += 1; return i; }
In the C++0x standard the usage of PRE_INC yields well-defined
result, as Igor already noticed. It is only POST_INC implies
undefined behavior.
In all the cases, in each one, whether its code reduction,
functional programming, Algebraic or RPN calculations, APL or
the like oriented languages, the results are ALL the same, but
different than:
x = x++ + y++;
and best from what I tell, its a MS Compiler issue only -
excused with the documented sequence point idea.
No, it is undefined for all other compilers, as well. The notion
of sequence point is common for all C/C++ compilers. However, the
exact place where sequence point occurs may vary from vendor to
vendor.
I think that B. Stroustrup answerd to you claim better than I
could:
"Why are some things left undefined in C++?"
http://www.research.att.com/~bs/bs_faq2.html#undefined
Alex
.
- Follow-Ups:
- Re: Same code and different result, Why?
- From: Tommy
- Re: Same code and different result, Why?
- References:
- Same code and different result, Why?
- From: Lorry Astra
- Re: Same code and different result, Why?
- From: Alex Blekhman
- Re: Same code and different result, Why?
- From: Tommy
- Re: Same code and different result, Why?
- From: Ulrich Eckhardt
- Re: Same code and different result, Why?
- From: Tommy
- Same code and different result, Why?
- Prev by Date: Re: Same code and different result, Why?
- Next by Date: Re: Same code and different result, Why?
- Previous by thread: Re: Same code and different result, Why?
- Next by thread: Re: Same code and different result, Why?
- Index(es):
Relevant Pages
|