1

§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?

4
  • 3
    Why are you asking us and not the authors of the spec? Commented Jan 22, 2011 at 11:27
  • 1
    possible duplicate of [Sequence point error: unspecified or undefined? ](open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#351) Commented Jan 22, 2011 at 11:28
  • 2
    Since you're quoting the C++ standard, it's not really a C question so I removed the tag. Commented Jan 22, 2011 at 11:35
  • possible duplicate of Why is i = ++i + 1 unspecified behavior? Commented Jan 22, 2011 at 11:37

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You should mention that this only affects the wording of “unspecified” vs. “undefined”, not the sequence point itself, which is fine.
@KonradRuddolph: Correct, I have clarified my answer.
6
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 

Comments

4

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.

8 Comments

The normative part of the standard is 'right' by definition, but the example is non-normative and has been acknowledged to have been wrong.
Oh, I didn't notice the 'unspecified' to 'undefined' change. Thanks.
Bizarrely, I don't think they're called sequence points in the C++ standard. They have the same concept (so it doesn't invalidate the answer) but I can't recall what they're called.
@paxdiablo : They are called sequence points and nothing else.
@paxdiablo: It's not in the new standard because the term is being deprecated, but I believe it's in the '03 standard (sorry, I don't have a copy on me to check).
|
2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.