2

Today, I noticed that it is possible to do something like this (C++):

int x = 3;
++++x; // Correct!
----x; // Correct, too!
++++++x; // x is now 6

That means we can put as many as pre-increments and pre-decrements together. Am I right? I know it will not be a good practice, however, should we use it or for example, does ++++x perform better than x += 2?

By the way, it is impossible to use post-increments and post-decrements in that way:

int x = 3;
x++++; // Error
x----; // Error

Why?

And my last question is, why it doesn't work in C#, Java, JavaScript, PHP or even C? (Note: I don't say Python and Ruby because they have none of postfix/prefix increment/decrement operators)

Thanks in advance!

MAChitgarha
  • 3,728
  • 2
  • 33
  • 40
  • 3
    fyi `++x` is pre-increment and `x++` is post-increment - you have the names reversed. ("pre" before, "post" after). Asking why it is different in other languages usually gets answered by: _"because they are other languages, if all languages were the same then there would only be one"_ – Richard Critten Apr 01 '18 at 09:42
  • @RichardCritten Thanks for your comment, I have edited it. And about languages, I know what do you want to say, but I say why it is possible only in C++ and not in other languages, or at least, other common languages? – MAChitgarha Apr 01 '18 at 09:47
  • 1
    does ++++x perform better than x += 2? Usually your compiler whould make the same thing out of this... So it doesn't make a difference. But ++++x looks shitty ;) – Thomas Apr 01 '18 at 09:55

2 Answers2

9

Both prefix and postfix increment need a reference to the value to be incremented.

++x returns an lvalue to the incremented x, which can be incremented again. That's why prefix increment can be chained.

Postfix x++ however, increments x and returns a temporary copy of the previous value, so the second postfix increment would apply to this temporary.

Performance is not relevant here. ++++x and x+=2 are equivalent - some compiler might generate better code for one or the other, but that's not expected, and even then the difference will be usually negligible on most execution platforms.

While ++++x is legal, it will usually be considered bad style.


As for why other languages don't do that: I am not aware of an rationale, though "don't help programmers write weird code" might be plausible. It might also simply be a side effect of other rules. Be aware that in other languages the expression evaluation rules are notably different. E.g., x = x++ + 1; ~~is~~ was undefined behavior in C and C++, but not in C#.

peterchen
  • 40,917
  • 20
  • 104
  • 186
  • "don't help programmers write weird code", if it will be true, then why that possibility exists in C++? – MAChitgarha Apr 01 '18 at 10:04
  • 1
    @MAChitgarha C++ evolved from C (which first appeared in 1972). There are lots of books on the history of, and comparison between programming languages; reading some of these would be a good place to start. – Richard Critten Apr 01 '18 at 10:19
  • @RichardCritten I don't want to compare languages, I want to know what benefit(s) this possibility has. – MAChitgarha Apr 01 '18 at 10:24
  • 1
    @MAChitgarha to find why it was added in the 1st place you need to read the history of (the language). To know why it is still present you need to think about backwards compatibility and the amount (man-centuries) of code that would need to be changed. – Richard Critten Apr 01 '18 at 10:26
  • @RichardCritten Don't you know about that? – MAChitgarha Apr 01 '18 at 10:28
  • 1
    @MAChitgarha I am not a computer language designer I am a software engineer using "what is". I know how I would find out the history (if I wanted to) and have shared this with you. It would require researching state of the art pre-1970 computer science and it's evolution during the 70s. – Richard Critten Apr 01 '18 at 10:31
  • 1
    I could say "because C++ is intended to be less restrictive" - but basically, @RichardCritten is right: this goes deeply into the design of languages and likely the result of multiple factors that annot be easily explained in SO format. – peterchen Apr 01 '18 at 10:40
  • 2
    Note that because of other changes, `x = x++ + 1;` now happens to be well defined for C++17. There is even [an example in the standard document](http://eel.is/c++draft/intro.execution#10). – Bo Persson Apr 01 '18 at 12:51
  • 1
    Nitpicking: Expressions can't have reference types. Pre-increment/decrement needs an lvalue, not a reference. – HolyBlackCat Oct 10 '22 at 09:29
1

There is no solid answer why this is not in java, C#. The designer of every language have their own choice, like no pointer in java. Also all the languages that you have mentioned does not create any binary file. They all need a virtual machine. So a exe created from C/C++ is totally different from a class file created for jvm.

Regarding performance of pre-increment. For every operation a C++ compiler generates assembly instruction. So for ++x and x = x + 1 compiler will generate same assembly instruction so performance speed will be same in both the case.

Also there is not preface answer why C++ compiler designer allowed ++++x; but not x++++;. Also this ++++x; is not a common practice in C++.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
  • OK, "the designer of every language have their own choice", I accept it about all languages except C, why there isn't this possibility in C? Isn't C++ a superset of C? – MAChitgarha Apr 01 '18 at 10:17
  • 1
    @MAChitgarha _"Isn't C++ a superset of C?"_ used to be, this is no longer true. – Richard Critten Apr 01 '18 at 10:20
  • 1
    According to the other answer, the the reason why ```x++++``` gives an error is well defined. – Robert Andrzejuk Apr 01 '18 at 12:46
  • "For every operation a C++ compiler generates assembly instruction." Not really. The assembly instructions together have to do what the C++ statements together mean, but that does not mean there's a simple requirement for each individual operation. For instance, optimizers are known to remove unnecessary instructions. – MSalters Oct 10 '22 at 10:08