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?

Philipp
  • 48,066
  • 12
  • 84
  • 109
Rob Harvey
  • 13
  • 2
  • 3
    Why are you asking us and not the authors of the spec? – BoltClock Jan 22 '11 at 11:27
  • 1
    possible duplicate of [Sequence point error: unspecified or undefined? ](http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#351) – CB Bailey Jan 22 '11 at 11:28
  • 2
    Since you're quoting the C++ standard, it's not really a C question so I removed the tag. – paxdiablo Jan 22 '11 at 11:35
  • possible duplicate of [Why is `i = ++i + 1` unspecified behavior?](http://stackoverflow.com/questions/1860461/why-is-i-i-1-unspecified-behavior) – CB Bailey Jan 22 '11 at 11:37

4 Answers4

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
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
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
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.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • The normative part of the standard is 'right' by definition, but the example is non-normative and has been acknowledged to have been wrong. – CB Bailey Jan 22 '11 at 11:31
  • Oh, I didn't notice the 'unspecified' to 'undefined' change. Thanks. – Peter Alexander Jan 22 '11 at 11:33
  • 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 Jan 22 '11 at 11:37
  • 1
    @paxdiablo : They are called [sequence points](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) and nothing else. – Prasoon Saurav Jan 22 '11 at 11:45
  • @Prasoon, you may want to look at the C++ standard. The phrase sequence point appears in there exactly zero times. – paxdiablo Jan 22 '11 at 12:03
  • 1
    @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). – Peter Alexander Jan 22 '11 at 12:05
  • @paxdiablo : Check out section 1.9/7 (C++03). – Prasoon Saurav Jan 22 '11 at 12:12
  • I stand corrected. Serve me right for looking at the wrong version of the standard :-) – paxdiablo Jan 22 '11 at 12:49
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.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214