Why is the priority of the postfix increment greater than the prefix increment?

It turned out that the postfix increment and decrement have a higher priority than the prefix (source). However, there is also an interesting phrase, the meaning of which I do not really understand: " The standard does not determine the order of priorities. They are derived from the grammar of the language."

If you look at a record like ++x++ and start choosing what the programmer wanted to use first, then you want to swap these priorities and first execute prefix. The logic is simple: each of these operators requires an lvalue, but only the prefix returns an lvalue. It turns out that the first calculation of the postfix operator gives rvalue and the prefix is no longer applicable.

What am I missing, and why are the priorities chosen this way?

I do not see any harm to compatibility with C, if the priorities are set like this:

screenshot of suggested priorities

PS: Based on question.

Author: Qwertiy, 2019-06-21

2 answers

C and C++ are different, but related languages that share common historical roots and very similar fundamental syntax. The basis of the C++ grammar came from the C language, where, as in C++, all postfix operators ([], (), ++, -> etc.) have a higher priority than all prefixes. (*, -, ++ etc.)

Neither C nor C++ has ever had any individual" prioritization " of operators. Operators appear in the grammar as whole classes of terminal symbols at once. In this case, these are the classes of postfix and prefix (aka unary ) operators.

However, your logic is not applicable in C. One of the fundamental differences between C and C++ is that C does not try to preserve the lvalue value of the results of operators without explicit necessity. In C, both postfix and prefix ++ returns rvalue.

So your question actually turns into a question about why the C++ grammar wasn't redone based on the considerations you gave about the fact that in C++ the prefix form ++ returns an lvalue. Answer: because the considerations given to you are not sufficient as a basis for such a rework. The negative effect of complicating grammar would be noticeable, while the positive effect would be negligible. And this is all just for the sake of being able to write ++x++ instead of (++x)++?

 3
Author: AnT, 2019-06-21 18:11:51

So it is easier for perception. Look at the priority table, first there are all postfix operators, then all prefix operators, then all binary operators. There was no compelling reason to make exceptions for ++ and --. You need to be a little unhealthy to write in the code ++x++.

 1
Author: Дмитрий Зиненко, 2019-06-21 16:11:59