gpt4 book ai didi

c++ - 如何确定作业的两个副作用是否未排序?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:27:38 24 4
gpt4 key购买 nike

来自 §5.2.6/1我们有(重点是我的):

The value of a postfix ++ expression is the value of its operand. [ Note: the value obtained is a copy of the original value — end note ] The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type other than cv bool, or a pointer to a complete object type. The value of the operand object is modified by adding 1 to it. The value computation of the ++ expression is sequenced before the modification of the operand object. With respect to an indeterminately-sequenced function call, the operation of postfix ++ is a single evaluation. [ Note: Therefore, a function call shall not intervene between the lvalue-to-rvalue conversion and the side effect associated with any single postfix ++ operator. — end note ] The result is a prvalue. The type of the result is the cv-unqualified version of the type of the operand. If the operand is a bit-field that cannot represent the incremented value, the resulting value of the bit-field is implementation-defined. See also [expr.add] and [expr.ass].

也就是说,操作数对象的修改顺序++表达式的值计算之后。

来自 §5.18/1我们有(重点是我的):

The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand. The result in all cases is a bit-field if the left operand is a bit-field. In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. With respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation. [ Note: Therefore, a function call shall not intervene between the lvalue-to-rvalue conversion and the side effect associated with any single compound assignment operator. — end note ]

assignment-expression:
      conditional-expression
      logical-or-expression assignment-operator initializer-clause
      throw-expression

assignment-operator: one of
      = *= /= %= += -= >>= <<= &= ^= |=

赋值也赋值运算符的左右操作数的值计算之后进行。

因此,如果我们考虑表达式

i = i++;

我们从 §5.2.6/1 知道表达式 i++ 的副作用,在这个赋值表达式的 RHS 上,在 i++ 的值计算之后排序.从 §5.18/1 中我们知道,赋值运算符的 LHS 上对应于对 i 赋值的副作用是在赋值运算符的左右操作数的值计算之后排序的。

但是我如何证明这两个副作用是未排序的,以证明表达式 i = i++; 显示未定义的行为?

最佳答案

首先,如果 A 未在 B 之前测序且 B 未在 A 之前测序,则 A 和 B 是未测序 (1.9/13)。这就是无序的定义。其次,如果对标量对象的两个副作用未排序,则行为未定义 (1.9/15)。因此,除非您能找到说明后增量和赋值已排序的内容(您不会),否则行为是未定义的。

关于c++ - 如何确定作业的两个副作用是否未排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35136597/

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