gpt4 book ai didi

c++ - 什么可以是子表达式?

转载 作者:行者123 更新时间:2023-11-30 02:32:03 27 4
gpt4 key购买 nike

我阅读了“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.

我还阅读了“序列点和表达式评估”视觉系统杂志,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++)

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

关于c++ - 什么可以是子表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36851412/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com