22

see simple example:

int a = 0;
int b = (a ++  ,  a + 1); // result of b is UB or well defined ?  (c++03).

This was changed in c++11/c++14 ?

Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
Khurshid
  • 2,654
  • 2
  • 21
  • 29
  • +1 for the nice question. Since all discussions I've seen here were about statements like `int b = ++a + a++;`. But you really need this in the code or this is just a curiosity? – VP. Jan 24 '14 at 08:00
  • 7
    I believe it's actually defined. The comma operator is a sequence point. – Mysticial Jan 24 '14 at 08:00
  • related http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – user2485710 Jan 24 '14 at 08:00
  • 1
    c++ has such more UB, I don't understand why. – Khurshid Jan 24 '14 at 08:01
  • @Khurshid C++ does indeed have a lot of UB. You just have to mess up harder to hit them. :) – Mysticial Jan 24 '14 at 08:02
  • 1
    I don't know the specifics of C++11 so I'll refrain from posting an answer. But I don't think there is any version of C or C++ where that row is well-defined. The comma operator should add a sequence point to prevent UB, but that doesn't mean that the code is _well-defined_. You still would have the order of evaluation of operands, which is _unspecified_ behavior. In other words, you can't know whether the sub-expression `a++` or the sub-expression `a + 1` gets evaluated first. – Lundin Jan 24 '14 at 08:02
  • 8
    @Lundin: Quoting the standard (C++11): "*A pair of expressions separated by a comma is evaluated left-to-right;*" – Michael Foukarakis Jan 24 '14 at 08:04
  • I know with comma operator, evaluation order is well defined, left to right. – Khurshid Jan 24 '14 at 08:04
  • from http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points `i = (++i,i++,i) // well defined ` -- right? – Khurshid Jan 24 '14 at 08:07
  • @MarcGlisse is it still "crappy code" if this appears inside the `for` initialization, condition and initilization parts? That's the normal place for it. – Brandin Jan 24 '14 at 08:36
  • @Brandin: Consider the fellow who will read that kind of code. Even if we can prove it's well defined, why confuse them without reason? The expression can be rewritten in a manner which will make it trivial to read and understand. – Michael Foukarakis Jan 24 '14 at 08:42
  • @mfukar Comma separated expressions appear all the time in `for` that doesn't make them automatically hard to understand. This particular example is probably just an artificial example to ask the particular point. It doesn't have any practical meaning so of course it is hard to "understand" – Brandin Jan 24 '14 at 08:46
  • 2
    @Brandin Yes. There are very, very few cases where the comma operator is appropriate, and the initialization of a `for` isn't one of them. (The "incrementation" expression in a `for` may be one, but the condition and the initialization certainly aren't.) – James Kanze Jan 24 '14 at 09:20
  • 2
    @Brandin The comma operator isn't that common, even in a `for`, and when it occurs, each sub-expression modifies a _different_ variable (e.g. `++ iter1, ++iter2`). While the statement in the example may be artificial, the issue clearly involves two sub-expressions of a comma operator which modify the same variable. Which is "crappy code", any way you look at it. – James Kanze Jan 24 '14 at 09:23
  • I don't understand why anyone would not expect this to work (who knows what the comma operator is)? – jwg Jan 24 '14 at 12:32
  • @JamesKanze I suppose it gets murkier if `iter2` was really a reference to `iter1`. Still rather funky code though. – Andre Kostur Jan 24 '14 at 16:00
  • @AndreKostur Yes. One sort of assumes that the author knew what he was doing, and that he followed good programming practices, but one never knows. – James Kanze Jan 24 '14 at 16:19

1 Answers1

29

The result is well defined and has been since C++98. The comma operator introduces a sequence point (or a "sequenced before" relationship in later C++s) between the the write and the second read of a and I don't see any other potential reasons for undefined behavior.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 8
    Note that the code in question is also perfectly legitimate in C, and gives defined results in C as well. – Jerry Coffin Jan 24 '14 at 08:03
  • 1
    @JerryCoffin: That is true. I restricted the domain of my "since" to refer to C++ versions only. – CB Bailey Jan 24 '14 at 08:05
  • 2
    I strongly *believe*, that code when most of people don't know for sure, others must add "i believe...", and only some know for sure is not *well-defined* – SChepurin Jan 24 '14 at 08:12
  • 3
    @SChepurin: Fortunately, there is an objective definition of well-defined C++ programs, the standard, which trumps all beliefs. :) – Michael Foukarakis Jan 24 '14 at 08:15
  • @mfukar - Unfortunately, *well-defined* is very often doesn't mean *well-written* code. If people stumble on some not important lines of the program, this can only mean *obfuscation* – SChepurin Jan 24 '14 at 08:19