Note that there are some explanatory texts on larger screens.

plurals
  1. POSo why is i = ++i + 1 well-defined in C++11?
    primarykey
    data
    text
    <p>I've seen the <a href="https://stackoverflow.com/q/1860461/150634">other</a> similar <a href="https://stackoverflow.com/q/4336860/150634">questions</a> and read the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#637" rel="nofollow noreferrer">defect</a> about it. But I still don't get it. Why is <code>i = ++i + 1</code> well-defined in C++11 when <code>i = i++ + 1</code> is not? How does the standard make this well defined?</p> <p>By my working out, I have the following <em>sequenced before</em> graph (where an arrow represents the sequenced before relationship and everything is a value computation unless otherwise specified):</p> <pre><code>i = ++i + 1 ^ | assignment (side effect on i) ^ ^ | | ☆i ++i + 1 || ^ i+=1 | ^ 1 | ★assignment (side effect on i) ^ ^ | | i 1 </code></pre> <p>I've marked a side effect on <code>i</code> with a black star and value computation of <code>i</code> with a white star. These appear to be unsequenced with respect to each other (according to my logic). And the standard says:</p> <blockquote> <p>If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.</p> </blockquote> <p>The explanation in the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#637" rel="nofollow noreferrer">defect report</a> didn't help me understand. What does the lvalue-to-rvalue conversion have to do with anything? What have I gotten wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload