title: Is it safe to put increment/decrement operators inside ternary/conditional operators?
body: <p>Here's an example</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  int main()
  {   
      int x = 0;
      cout &lt;&lt; (x == 0 ? x++ : x) &lt;&lt; endl; //operator in branch
      cout &lt;&lt; "x=" &lt;&lt; x &lt;&lt; endl;
      cout &lt;&lt; (x == 1 || --x == 0 ? 1 : 2) &lt;&lt; endl; //operator in condition
      cout &lt;&lt; "x=" &lt;&lt; x &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>0
  x=1
  1
  x=1
  </code></pre>
  
  <p>I understand the output, but <em>is this undefined behaviour or not? Is the order of evaluation guaranteed in either case?</em></p>
  
  <p>Even if guaranteed, I'm quite aware using increment/decrement can quickly become an issue for readability. I only ask as I saw similar code and was immediately unsure, given there are lots of examples of ambiguous/undefined use of increment/decrement operators, such as...</p>
  
  <ul>
  <li><blockquote>
    <p>C++ does not define the order in which function parameters are evaluated. <a href="http://www.learncpp.com/cpp-tutorial/33-incrementdecrement-operators-and-side-effects/">↪</a></p>
  </blockquote>
  
  <pre><code>int nValue = Add(x, ++x);
  </code></pre></li>
  <li><blockquote>
    <p>The C++ language says you cannot modify a variable more than once between sequence points. <a href="http://www.parashift.com/c++-faq-lite/double-mod-betw-seq-pt.html">↪</a></p>
  </blockquote>
  
  <pre><code> x = ++y + y++
  </code></pre></li>
  <li><blockquote>
    <p>Because increment and decrement operators have side effects, using expressions with increment or decrement operators in a preprocessor macro can have undesirable results. <a href="http://msdn.microsoft.com/en-us/library/dy3d35h8.aspx">↪</a></p>
  </blockquote>
  
  <pre><code> #define max(a,b) ((a)&lt;(b))?(b):(a)
   k = max( ++i, j );
  </code></pre></li>
  </ul>