0

I almost pulled all of my hair. The code speaks for itself:

const float* const vertices = ...;
unsigned int j = 0;

        pair<float, float> poor;
        poor.first = vertices[j];
        poor.second = vertices[j + 1];
        CCLOG("WIT0 %f,%f", poor.first, poor.second);
        poor.first = vertices[j++];
        poor.second = vertices[j++];
        CCLOG("WIT1 %f,%f", poor.first, poor.second);

        j -= 2;

        poor = pair<float, float>(vertices[j], vertices[j + 1]);
        CCLOG("WIT2 %f,%f", poor.first, poor.second);
        poor = pair<float, float>(vertices[j++], vertices[j++]);
        CCLOG("WIT3 %f,%f", poor.first, poor.second);

The result is:

WIT0 -38.063835,32.743618
WIT1 -38.063835,32.743618
WIT2 -38.063835,32.743618
WIT3 32.743618,-38.063835 <- What about that, eh?

I always, always thought code is evaluated from left to right. Seems this isn't the case here. Code run on Visual Studio 2015, Win32 Debug mode.

Can someone tell me if this is implementation specific and not addressed yet by the C++ guide?

Bill Kotsias
  • 3,258
  • 6
  • 33
  • 60
  • Sure, but mine is better phrased – Bill Kotsias Apr 25 '17 at 13:54
  • 1
    "I always, always thought code is evaluated from left to right." you thought wrong. – Slava Apr 25 '17 at 13:55
  • What is pretty confusing in this situation is that the "comma operator" *IS* a sequence point. But the comma in function call arguments *IS NOT* a sequence point. – Zan Lynx Apr 25 '17 at 13:56
  • Actually this is not exact duplicate, as it would have UB unrelated if ordere of evaluation of function arguments would be defined. – Slava Apr 25 '17 at 13:57
  • 1
    It's not just the order of evaluation, but [having two `j++` in the same expression](http://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior) is a big no-no. – Bo Persson Apr 25 '17 at 14:00
  • Please make up your mind if it's duplicate or not, so I take according action. Thanks. – Bill Kotsias Apr 25 '17 at 14:01
  • @BoPersson Oh, why is that? It's pretty clear to me. – Bill Kotsias Apr 25 '17 at 14:01
  • It is not up to me, I did not vote for duplicate. But you have 2 issues - order of evaluation, and UB because you modify and read a variable without sequence point. (in this case you modify and read twice) – Slava Apr 25 '17 at 14:02
  • @Bill - Check the link. :-) – Bo Persson Apr 25 '17 at 14:02
  • @BoPersson I did. THAT's not clear code. – Bill Kotsias Apr 25 '17 at 14:04
  • @BillKotsias You cannot have 2 `j++` without a sequence point btw them. What is not clear to you? Even if you had one `j++` and use `j` in another - that is still UB. – Slava Apr 25 '17 at 14:08
  • @Slava The sequence point? http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – Bill Kotsias Apr 25 '17 at 14:09
  • @BillKotsias yes https://en.wikipedia.org/wiki/Sequence_point – Slava Apr 25 '17 at 14:10

0 Answers0