2

I saw this question been asked at SO few hours ago.

My question is related to it but just a bit different and is a result of Johannes Schaub-litb's comment to Oli Charlesworth's answer.

Is i = (0, ++i, 0) undefined behavior?

P.S: This is just for educational purpose and has no relation with real life code or examples. Please ignore this question if you just want to add comments like "Why do you care? Nobody uses it in real world applications." etc

TIA

Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 1
    this is answered in your linked question: http://stackoverflow.com/questions/3958878/is-i-0-i-defined/3959493#3959493 – Matt Ellen Oct 18 '10 at 16:51
  • There seem to be about a million of this sort of question on SO recently. I'm surprised there aren't more compiler developers on here; they ought to be able to answer the questions pretty quickly and definitively! – Oliver Charlesworth Oct 18 '10 at 16:52
  • 2
    @Oli: No, we don't need compiler developers, we need standards-commitee members. Obviously their phrasing has arisen millions of questions, both in C++03 and C++0x. What needs to be done, is that one of them write a small essay about what they really intended to say, in great detail and abundant in examples. – Armen Tsirunyan Oct 18 '10 at 17:10
  • @Armen: True. But I imagine compiler developers have had to give this sort of issue some serious thought before. As opposed to everyone else, who'll just write an answer based on intuition: "I think it is defined!" or "I think it is undefined!". – Oliver Charlesworth Oct 18 '10 at 17:11
  • 1
    My comment is who cares. This will never be in real code. If you ever have to ask a member of the standards committee is this valid then you should not be doing it. – Martin York Oct 18 '10 at 18:17

3 Answers3

11

No, it is not undefined behavior.

The only potential for undefined behavior here is the multiple modifications of i, but the one inside the () is isolated from the other by sequence points inherent in , operator. So, no undefined behavior.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

I think that in i = (0, ++i, 0), i is modified at different sequence points, because of the comma operator, so therefore the behavior is defined.

Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
1

If i = (++i, 0) is defined, then so is this. Perhaps you should simplify the question!

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680