24

Looks like we're getting a whole new breed of "interview questions" for C++ (I hope not, actually).

It is known to be undefined behavior prior to C++17, but will it be well-defined from C++17 onward?

Since at the moment there doesn't seem to be a compiler that implements this C++17 modification, can anyone explain what will, according to expression evaluation rules, the value of x be in the following code?

int i = 0;
int x = i++ + i++;

Alisdair Meredith mentions this example here in his CppCon 2016 talk, but it's not entirely clear to me what the final value of x will be (although it seems what he's saying is that it'll be at least 1).

Obviously, i itself will in that case be 2 at the end of the expression.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Leo Heinsaar
  • 3,887
  • 3
  • 15
  • 35
  • You might want to read e.g. [this sequencing and evaluation order reference](http://en.cppreference.com/w/cpp/language/eval_order). – Some programmer dude Sep 30 '16 at 17:23
  • 4
    @LeoHeinsaar Seems we have to adjust our reflexes in future ;P ... – πάντα ῥεῖ Sep 30 '16 at 17:36
  • 1
    @πάνταῥεῖ I understand, don't even worry. It's all right. :-) – Leo Heinsaar Sep 30 '16 at 17:36
  • 2
    It would be even better if someone could explain how this will be useful. – Bo Persson Sep 30 '16 at 17:40
  • @BoPersson I don't know the specifics, but I'm guessing that it won't be particularly useful in this particular scenario, but taking `i` as being of a user-defined type, it might in fact be useful. Maybe. Also, by extension, the venerable `i+++++i` should become legal, right? – Williham Totland Sep 30 '16 at 17:42
  • @WillihamTotland No, because it still lexes as `i ++ ++ + i` and you can't increment the rvalue `i++`. – aschepler Sep 30 '16 at 17:46
  • @BoPersson the only rationale is to confuse an interviewee and recover if (s)he reads papers thoroughly – Serge Sep 30 '16 at 17:47
  • @aschepler Ah, true. – Williham Totland Sep 30 '16 at 17:49
  • The answer to the interviewer is to just say it is undefined. If they object, just state "I disagree with you until you show in the standard as of C++17 where it is defined". Let *the interviewer* show you the research if they want to throw this type of question at interviewees. – PaulMcKenzie Sep 30 '16 at 17:57
  • the post-increment is the post-increment and the pre-increment is the pre-increment operator x is simply 0 – Raindrop7 Sep 30 '16 at 19:19
  • 1
    In the unlikely event such a question would be asked in an interview, I'd rather the candidate answered: "even if it somehow became well defined at some point in the future, such code should still be avoided in our code base". – Marc Glisse Sep 30 '16 at 19:22
  • And, any C++ techie with self-esteem should avoid such employers altogether. – Seshadri R Oct 20 '16 at 12:15

1 Answers1

18

P0145R3 (PDF) does not change the evaluation order of all expressions. It only affects a small number of operators. And binary addition is not on that list.

Therefore the above code remains undefined.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    But Alisdair explicitly says in his slide it's *well-defined*. I doubt it's an oversight, or is it? – Leo Heinsaar Sep 30 '16 at 17:44
  • 2
    I'm sure Alisdair said that, but I'm just as sure that P0145R3 doesn't agree with him. An older version of P0145 did indeed offer ordering to binary operators in general, but the current revision is more limited in scope. So I'm going with what's actually written in the paper, rather than on some slide. – Nicol Bolas Sep 30 '16 at 17:45
  • 1
    @LeoHeinsaar Look at section 5 of the PDF. – NathanOliver Sep 30 '16 at 17:46
  • @NathanOliver Did that section of the paper go into the standard as it was written? Maybe there was a change? If not, looks like Nicol is right and it might be an oversight by Alisdair. – Leo Heinsaar Sep 30 '16 at 17:49
  • I'm leaning towards an oversight. My look through of the [current draft of C++17](http://eel.is/c++draft/) did not show me any language that would make this work now. – NathanOliver Sep 30 '16 at 17:50
  • @NathanOliver Strange. Ok, I'll wait a bit then and if it turns out to be an oversight indeed, I'll rephrase the question so it doesn't confuse anyone. (He seems pretty confident, explicitly walking through the evaluation.) – Leo Heinsaar Sep 30 '16 at 17:54
  • 1
    `+` wasn't touched. `i++ << i++` is well-defined now, though. – T.C. Oct 01 '16 at 02:54