什么可以是子表达式?

What can be a subexpression?

我读了 "C++.Primer plus. Stephen Prata"(第 6 版)。 第 209 页是:

y = (4 + x++) + (6 + x++);

The expression 4 + x++ is not a full expression, so C++ does not guarantee that x will be incremented immediately after the subexpression 4 + x++ is evaluated. Here the full expression is the entire assignment statement, and the semicolon marks the sequence point, so all that C++ guarantees is that x will have been incremented twice by the time the program moves to the following statement. C++ does not specify whether x is incremented after each subexpression is evaluated or only after all the expressions have been evaluated, which is why you should avoid statements of this kind.

我还阅读了 "Sequence Points and Expression Evaluation" Visual Systems Journal,2002 年 8 月。Klaus Kreft 和 Angelika Langer。 有:

 x[i]=i++ + 1; 

Let's assume variable i has the value 1 before we enter the statement. What will be the result of evaluation of this expression? The correct answer is: we don't know. However, programmers ever too often believe that they know what this program fragment does. Typical answers include: "x[1] will have the value 2", or "x[2] will have the value 2", or even "x[1] will have the value 3".

The third option is definitely wrong. This will not happen because i++ is a postfix increment and returns i's initial value 1; hence the value of the right hand side of the assignment is 2, and definitely not 3. [...] So far so good, but we do not know which entry of the array x will be modified. Will the index be 1 or 2 when the right hand side value will be assigned to x[i]?

There is no definite answer to this question. It fully depends on the order in which the compiler evaluates the subexpressions. If the compiler starts on the right hand side of the assignment and evaluates i++ + 1 before it figures out which position in the array x must be assigned to then x[2] will be modified because i will have already been incremented in the course of evaluating the subexpression i++. Conversely, if the compiler starts on the left hand side and figures out that it must assign to position i in array x, which at that time will still be position 1, before it evaluates the right hand side then we'll end up with a modification of x[1]. Both outcomes are equally likely and equally correct. "

如何理解子表达式在哪里? 4 + x++6 + x++ 是子表达式,因为它们在圆括号中? x[i]i++ + 1 是子表达式?为什么? 我对此很感兴趣,因为我想了解假设中可能发生副作用的地方。

将其分解,行

y = (4 + x++) + (6 + x++);

是一个表达式语句。这样的东西由一个表达式后跟一个;组成,所以

y = (4 + x++) + (6 + x++)

是一个表达式。

因为这个表达式不是另一个表达式的一部分(但只是表达式-语句),它是一个 完整表达式 。另一方面, 子表达式 另一个表达式的一部分的表达式。在下文中,我将使用大写字母来命名表达式,而不是 C++ 标识符。

上面的完整表达式是以下形式的赋值表达式:

y = A

其中 A 是剩余的加法表达式

(4 + x++) + (6 + x++)

加法表达式的形式是X + Y,所以我们将其分解为两个表达式

(4 + x++)
(6 + x++)

第一个由 (Z) 形式的表达式组成,其中 Z4 + x++。而 4 + x++ 由两个表达式 4x++ 组成。等等。所有这些表达式都是

的一部分
y = (4 + x++) + (6 + x++)

因此它们是上述表达式的子表达式

What can be a subexpression?

Any expression 可以是子表达式。虽然,某些表达式可能不是某些其他表达式的子表达式。

4 + x++ and 6 + x++ are subexpressions

正确。这两个都是算术表达式,加法更具体。

because they are into round brackets?

嗯,有点。在括号内,它们确实是括号表达式的子表达式。

一般来说,它们是子表达式,因为它们是表达式,但也是另一个表达式的一部分。

x[i] and i++ + 1 are subexpression? Why?

是的,他们是。参见

这是一个方便的列表,列出了所有可能的 expressions 在 c++ 中。

让我们找出y = (4 + x++) + (6 + x++);中的子表达式。第一个没有子表达式的表达式是 4。这是一个字面意思。它是 4 + x++ 的一个子表达式,它是一个加法。添加的形式为 A + B。在这种情况下,子表达式 A 是 4,子表达式 B 是 x++,这是一个 post 增量。哦,但是它也包含一个子表达式:x。它是一个标识符,不包含子表达式。 4 + x++ 是括号表达式 (4 + x++) 的子表达式。那是 (4 + x++) + (6 + x++) 的子表达式,它是 y = (4 + x++) + (6 + x++); 的子表达式,它是一个赋值。赋值是一个完整的表达式——而不是子表达式。我留下了一些未探索的子表达式,我将把它们留作 reader.

的练习