title: Undefined behavior and sequence points reloaded
body: <p>Consider this topic a sequel of the following topic:</p>
  
  <blockquote>
    <p><strong>Previous installment</strong> <br/>
    <em><a href="https://stackoverflow.com/questions/4176328">Undefined behavior and sequence points</a></em></p>
  </blockquote>
  
  <p>Let's revisit this <em>funny</em> and <em>convoluted</em> expression (the italicized phrases are taken from the above topic *smile* ):</p>
  
  <pre><code>i += ++i;
  </code></pre>
  
  <p>We say this invokes undefined-behavior. I presume that when say this, we implicitly assume that <em>type</em> of <code>i</code> is one of the built-in types.</p>
  
  <p>What if the <em>type</em> of <code>i</code> is a user-defined type? Say its type is <code>Index</code> which is defined later in this post (see below). Would it still invoke undefined-behavior?</p>
  
  <p>If yes, why? Is it not equivalent to writing <code>i.operator+=(i.operator++());</code> or even syntactically simpler  <code>i.add(i.inc());</code>? Or, do they too invoke undefined-behavior?</p>
  
  <p>If no, why not? After all, the object <code>i</code> gets modified <strong>twice</strong> between consecutive sequence points. Please recall the rule of thumb: <a href="http://msdn.microsoft.com/en-us/library/d45c7a5d%28v=vs.90%29.aspx" rel="noreferrer">an expression can modify an object's value only once between consecutive "sequence points</a>. And if  <code>i += ++i</code> is an expression, then it must invoke undefined-behavior. If so, then its equivalents <code>i.operator+=(i.operator++());</code> and  <code>i.add(i.inc());</code> must also invoke undefined-behavior which seems to be untrue! (as far as I understand)</p>
  
  <p>Or, <code>i += ++i</code> is not an <em>expression</em> to begin with? If so, then what is it and what is the definition of <em>expression</em>?</p>
  
  <p>If it's an expression, and at the same time, its behavior is <strong>also</strong> well-defined, then it implies that the number of sequence points associated with an expression somehow depends on the <em>type</em> of operands involved in the expression. Am I correct (even partly)?</p>
  
  <hr>
  
  <p>By the way, how about this expression?</p>
  
  <pre><code>//Consider two cases:
  //1. If a is an array of a built-in type
  //2. If a is user-defined type which overloads the subscript operator!
  
  a[++i] = i; //Taken from the previous topic. But here type of `i` is Index.
  </code></pre>
  
  <p>You must consider this too in your response (if you know its behavior for sure). :-)</p>
  
  <hr>
  
  <p>Is</p>
  
  <pre><code>++++++i;
  </code></pre>
  
  <p>well-defined in C++03? After all, this is this,</p>
  
  <pre><code>((i.operator++()).operator++()).operator++();
  </code></pre>
  
  <hr>
  
  <pre><code>class Index
  {
      int state;
  
      public:
          Index(int s) : state(s) {}
          Index&amp; operator++()
          {
              state++;
              return *this;
          }
          Index&amp; operator+=(const Index &amp; index)
          {
              state+= index.state;
              return *this;
          }
          operator int()
          {
              return state;
          }
          Index &amp; add(const Index &amp; index)
          {
              state += index.state;
              return *this;
          }
          Index &amp; inc()
          {
              state++;
              return *this;
          }
  };
  </code></pre>