Reputation: 13
§5/4 C++ standard
i = 7, i++, i++; // i becomes 9
i = ++i + 1; //the behavior is unspecified
That should be changed to
i = 7, i++, i++; // the behavior is undefined
i = ++i + 1; //the behavior is undefined
right?
Upvotes: 1
Views: 183
Reputation: 92854
i = 7, i++, i++; // i becomes 9
is perfectly fine. Operator =
has higher precedence than ,
so the expression is equivalent to
(i = 7), i++, i++;
which is perfectly well defined behaviour because ,
is a sequence point.
As far as
i = ++i + 1; //the behavior is unspecified
is concerned the behaviour is undefined in C++03 but well defined in C++0x. If you have the C++0x draft you can check out in section 1.9/15
i = i++ + 1; // the behavior is undefined
Upvotes: 6
Reputation: 545508
That should be changed to
i = 7, i++, i++; // the behavior is undefined
Why? The standard is correct, this shouldn’t be changed, and this behaviour is well-defined.
A comma (,
) introduces a sequence point into the calculation so the order of execution is defined.
Upvotes: 2
Reputation: 791411
Yes, please see this defect report: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#351 .
Clarification: the example is wrong but your 'fix' is incorrect for the first statement. The first statement is correctly commented in the standard. It is only the second comment that is inaccurate.
Upvotes: 7
Reputation: 54270
No, the standard is right. The comma operator guarantees that any side effects of previous operands are completed before evaluating the next.
These guarantees are provided by sequence points, which the comma operator (as well as &&
and ||
) are.
Note that you are correct on the wording change for the second statement. It is undefined, not unspecified.
Upvotes: 4