Saturday, March 23, 2019

c# - Difference in output of program

The designers of C# and C++ and C made different decisions about how to handle order of evaluation whereas in C# the order of evaluation if from left to right:



Operands in an expression are evaluated from left to right.



and side effects take place from left to right as well.


In C++ this is not the case, the draft C++ standard section 1.9 Program execution paragraph 15 says:



Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced.



and if side effects on the same scalar object is sequenced relative to another side efect the result it undefined behavior:



[...]If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.[...]



and the draft gives examples of undefined behavior:


i = i++ + 1; // the behavior is undefined

which if we look at the expression from your code:


i += i++;

which is equivalent to:


i = i + i++ ;

we can now clearly see it invokes undefined behavior which means the result is unpredictable and can not be relied on.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...