c++ spaces in operators , what are the rules

↘锁芯ラ 提交于 2019-12-13 13:52:58

问题


Does spaces have any meaning in these expressions:
assume:

int a = 1;  
int b = 2;  

1)

int c = a++ +b;

Or,
2)

int c = a+ ++b;

When I run these two in visual studio, I get different results. Is that the correct behavior, and what does the spec says?
In general, what should be evaluated first, post-increment or pre-increment?

Edit: I should say that
c =a+++b;
Does not compile on visual studio. But I think it should. The postfix++ seems to be evaluated first.


回答1:


Is that the correct behavior

Yes, it is. Postfix ++ first returns the current value, then increments it. so int c = a++ +b means compute the value of c as the sum between current a(take the current a value, and only after taking it, increment a) and b; Prefix ++ first increments the current value, then returns the value already incremented, so in this case, int c = a+ ++b means compute c as the sum between a and the return of the next expression, ++b, which means b is first incremented, then returned.

In general, what should be evaluated first, post-increment or pre-increment?

In this example, it is not about which gets evaluated first, it is about what each does - postfix first returns the value, then increments it; prefix first increments the value, then returns it.

Hth




回答2:


Maybe it helps to understand the general architecture of how programs are parsed.

In a nutshell, there are two stages to parsing a program (C++ or others): lexer and parser.

The lexer takes the text input and maps it to a sequence of symbols. This is when spaces are handled because they tell where the symbols are. Spaces really matter at some places (like between int and c, to not confuse with the symbol intc) but not others (like between a and ++ because there is no ambiguity to separate them).

The first example:

int c = a++ +b;

gives the following symbols, each on its own row (implementations may do this in slightly different ways of course):

int
c
=
a
++
+
b
;

While in the other case:

int c = a+ ++b;

the symbols are instead:

int
c
=
a
+
++
b
;

The parser then builds a tree (Abstract Syntax Tree, AST) out of the symbols and according to some grammar. In particular, according to the C++ grammar, + as an addition has a lower precedence than the unary ++ operator (regardless of postfix or prefix). This means that the first example is semantically the same as (a++) + b while the second is like a+ (++b).

For your examples, the ASTs will be different, because the spaces already lead to a different output at the lexer phase.

Note that spaces are not required between ++ and +, so a+++b would theoretically be fine, but this is not recommended for readability. So, some spaces are important for technical reasons while others are important for us users to read the code.




回答3:


Yes they should be different; the behaviour is correct.
There are a few possible sources for your confusion.


This question is not about "spaces in operators". You have different operators. If you were to remove the space, you would have a different question. See What is i+++ increment in c++

It's also not about "what should be evaluated first, post-increment or pre-increment". It's about understanding the difference between post-increment and pre-increment.

  • Both increment the variable to which they apply.
  • But the post-increment expression returns the value from before the increment.
  • Whereas the pre-increment expression returns the value after the increment.

I.e.

//Given:
int a = 1;  
int b = 2; 

//Post-increment
int c = a++ +b; =>
        1 + 2; (and a == 2) =>
        3;

//Pre-increment
int c = a+ ++b; =>
        1 + 3; (and b == 3) =>
        4;

Another thing that might be causing confusion. You wrote: a++ +b;. And you may be assuming that +b is the unary + operator. This would be an incorrect assumption because you have both left and right operands making that + a binary additive operator (as in x + y).


Final possible confusion. You may be wondering why:

  • in a++ +b the ++ is a post-increment operator applied to a.
  • whereas in a+ ++b it's a pre-increment operator applied to b.

The reason is that ++ has higher precedence than the binary additive +. And in both cases it would be impossible to apply ++ to +.



来源:https://stackoverflow.com/questions/42159099/c-spaces-in-operators-what-are-the-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!