2

I thought the only difference between prefix ++ and postfix ++ is their precedence. Obviously I was wrong. Perhaps it's the returned value that matters?

Rapptz
  • 20,807
  • 5
  • 72
  • 86
Felix
  • 209
  • 1
  • 3
  • 8

2 Answers2

14

In C++, ++++i compiles, because the prefix operator++ returns an lvalue. However, it is still hardly advisable to use this madness-increment operator, because it invokes UB.

I'm a C guy and this is was tagged C as well, so it's worth pointing out that this is different in C, in which case the answer is:

No, ++++i isn't permitted either.

That's all I've got.

Ben Collins
  • 20,538
  • 18
  • 127
  • 187
  • @FDinoff I don't speak C++ :) –  Aug 06 '13 at 19:31
  • 3
    @FDinoff: It (probably) compiles and runs in most C compilers - doesn't mean it's "valid and legal" - or necessarily that it gives a correct answer (based on the assumption that what we actually want is to achieve `i+=2` when the keyboard doesn't have a 2, or whatever the excuse for such code is). – Mats Petersson Aug 06 '13 at 19:38
  • It's from c++ in fact and ++++i actually works while ++i++ results in "error: lvalue required as increment operand" – Felix Aug 06 '13 at 19:39
  • @MatsPetersson "It (probably) compiles and runs in most C compilers" - it doesn't, it's an error. –  Aug 06 '13 at 19:41
  • Ok, but either way, it's WRONG - it's undefined behaviour to modify a variable twice -> unpredictable results -> don't write code like that. – Mats Petersson Aug 06 '13 at 19:42
  • @MatsPetersson It is wrong, so is the assertion that "it compiles in C", because it doesn't. –  Aug 06 '13 at 19:46
  • @triclosan Most of those questions is "why does `int a[10]; return a;` segfault?" :) I. e., nothing to do with C++. –  Aug 06 '13 at 19:52
  • I was starting to wonder what it was called ("Madness increment operator"). Thanks for that edit! – Floris Velleman Aug 06 '13 at 20:01
  • @Ben's edit looks fine to me - this appears to be a C++ question, so it behooves you to answer in terms of C++ *first*. The C note is useful as well, of course. If you disagree *strongly*, roll it back - but please let's keep the commentary relevant to the question and answer as it exists *now* - it's getting pretty noisy. Tangentially-relevant: http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions – Shog9 Aug 06 '13 at 20:31
  • @Shog9 (Of course I am not surprised you agree with another mod's edit...) - I'm not rolling it back anymore (despite I *strongly* disagree), because then he will roll it back again, and the game goes on, and you're right, we don't need that. But only if he didn't give a revenge downvote! That would have been mature-ish (sort of). –  Aug 06 '13 at 20:33
  • @Ben's not a mod (neither am I, strictly-speaking). And no, he wouldn't get into a rollback war with you over it - his goal here was to *limit* the drama not contribute to it. No revenge-votes from us - that'd be childish (and we have better ways of getting revenge anyway). – Shog9 Aug 06 '13 at 20:37
  • @Shog9 Nice to see that, and thanks for the clarification. (Euh, I wish everybody had the communication skills and patience you have...) –  Aug 06 '13 at 20:40
  • Does `++++i` in C++ really invoke undefined behaviour? If the prefix increment operator returns a reference to the incremented variable (as I'm led to believe it does), then it's perfectly safe, isn't it? –  Aug 06 '13 at 20:55
  • @joesavage please read any of the hundred answers on "UB" and sequence points... e.g. http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – sehe Aug 06 '13 at 20:56
  • @joesavage I think a sequence point is missing, but I may very well be wrong. Would you happen to have a Standards quote or any other reference? I'm happy to edit my question (***until I can...***) –  Aug 06 '13 at 20:56
  • You are indeed correct, there's a sequence point missing as the incrementation operators are only guaranteed to work between sequence points (so the reference that's returned from the first "increment" could in reality have been incremented or not -- it's undefined). –  Aug 06 '13 at 21:01
8

Both wrong actually (as responded before), but 1st even is not compilable, cuz postfix ++ needs an lvalue, but prefix ++ returns temporary. the second one, despite of comilability, is a violation of the rule: between sequence points you can modify a variable only once. here you, obviously, trying to modify i twice!

zaufi
  • 6,811
  • 26
  • 34