Alex Rivera | Logout

Is the behaviour of i = i++ really undefined?

Asked 2011-02-11T12:14:04.897
23

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

According to c++ standard,

i = 3;
i = i++;

will result in undefined behavior.

We use the term "undefined behavior" if it can lead to more then one result. But here, the final value of i will be 4 no matter what the order of evaluation, so shouldn't this really be called "unspecified behavior"?

Edit
Report

1 Answer

4

i=, and i++ are both side effects that modify i.

i++ does not imply that i is only incremented after the entire statement is evaluated, merely that the current value of i has been read. As such, the assignment, and the increment, could happen in any order.

answered 2011-02-11T12:19:16.713

Your Answer