那么为什么i = ++ i + 1在C ++ 11中定义良好?

问题描述:

我看过其他类似问题,并阅读缺陷它。但我还是不明白。为什么 i = i ++ + 1 是在C ++ 11中定义的 i = ++ i + 1 不?

I've seen the other similar questions and read the defect about it. But I still don't get it. Why is i = ++i + 1 well-defined in C++11 when i = i++ + 1 is not? How does the standard make this well defined?

通过我的锻炼,我在图表之前有以下顺序(其中箭头代表顺序)除非另有说明,否则关系和一切都是值计算):

By my working out, I have the following sequenced before graph (where an arrow represents the sequenced before relationship and everything is a value computation unless otherwise specified):

i = ++i + 1
     ^
     |
assignment (side effect on i)
 ^      ^
 |      |
☆i   ++i + 1
     ||    ^
    i+=1   |
     ^     1
     |
★assignment (side effect on i)
  ^      ^
  |      |
  i      1

我在 / code>与一个黑星和价值计算 i 与一个白星。这些看起来不是彼此相关的(根据我的逻辑)。而标准说:

I've marked a side effect on i with a black star and value computation of i with a white star. These appear to be unsequenced with respect to each other (according to my logic). And the standard says:


如果对标量对象的副作用相对于对同一标量对象的另一个副作用

完整报告没有帮助我理解。左值到右值转换与什么有关?我错了什么?

The explanation in the defect report didn't help me understand. What does the lvalue-to-rvalue conversion have to do with anything? What have I gotten wrong?


...或值计算 使用相同标量对象的值

... or a value computation using the value of the same scalar object ...

重要部分在此处加粗。左侧不使用 i 的值进行值计算。计算的是 glvalue 。只有之后(排序后),对象的值被触摸和替换。

The important part is bolded here. The left hand side does not use the value of i for the value computation. What is being computed is a glvalue. Only afterwards (sequenced after), the value of the object is touched and replaced.

不幸的是,这是一个非常微妙的点:)

Unfortunately this is a very subtle point :)