title: Usage of prefix or postfix increment (or decrement)
body: <p>I've found this code is as an example of the usage of the comma-operator in a C++ book (C++ Primer, 5th Edition):</p>
  
  <pre><code>vector&lt;int&gt;::size_type cnt = ivec.size();
  // assign values from size...1 to the elements in ivec
  for(vector&lt;int&gt;::size_type ix = 0; ix != ivec.size(); ++ix, --cnt)
      ivec[ix] = cnt;
  </code></pre>
  
  <p>I don't think that this is an appropriate example, because the <a href="http://en.cppreference.com/w/cpp/language/eval_order" rel="noreferrer" title="order of evalution">order of evalation</a> and side-effects don't matter here. The comma-operator just allows for separating the increment and decrement expressions, which is a common usage of the comma operator, but not the intention of this section in the book. A better <a href="http://en.cppreference.com/w/cpp/language/operator_other" rel="noreferrer" title="better">example</a> is shown at <a href="http://en.cppreference.com/w/cpp/language/operator_other" rel="noreferrer">cppreference.com</a> (please scroll down to the headline <strong>Built-in comma operator</strong>).</p>
  
  <p>What is really on my mind is the following exercise:  </p>
  
  <blockquote>
    <p><strong>Exercise 4.31</strong> The program in this section used the prefix increment and decrement operators. Explain why we used prefix and not
    postfix. What changes would have to be made to use the postfix
    versions? Rewrite the program using the postfix operators.</p>
  </blockquote>
  
  <p>There is no special reason to prefer the prefix over the postfix operators in this case. The evaluation order doesn't matter. The manipulation of objects with <code>simple</code> type like <code>vector&lt;int&gt;::size_type</code> shouldn't benefit from the usage of prefix over postfix in practice, so it's just a known convention to prefer prefix over postfix, in case the performance matters.</p>
  
  <p>To give you the full context, here the section from the book:</p>
  
  <blockquote>
    <p><strong>4.10 Comma Operator</strong><br>
    The <strong>comma operator</strong> takes two operands, which it evaluates from left to right. Like the logical <em>AND</em> and
    logical <em>OR</em> and the conditional operator, the comma operator
    guarantees the order in which its operands are evaluated.</p>
    
    <p>The left-hand expression is evaluated and its result is discareded.
    The result of a comma expressions is the value of its right-hand
    expression. The result is an lvalue if the right-hand operand is an
    lvalue. One common use for the comma operator is in a for loop:  </p>
  
  <pre><code>vector&lt;int&gt;::size_type cnt = ivec.size();  
  // assign values from size...1 to the elements in ivec  
  for(vector&lt;int&gt;::size_type ix = 0; ix != ivec.size(); ++ix, --cnt)  
      ivec[ix] = cnt;  
  </code></pre>
    
    <p>This loop increments <em>ix</em> and decrements <em>cnt</em> in the expression in
    the for header. Both <em>ix</em> and <em>cnt</em> are changed on each trip through
    the loop. As long as the test of <em>ix</em> succeeds, we reset the
    <s><a href="http://ptgmedia.pearsoncmg.com/images/9780321714114/errata/9780321714114_errata_10-31-12.html" rel="noreferrer" title="next">next</a></s> current element to the current value of <em>cnt</em>.</p>
  </blockquote>
  
  <p>Am I right? Or do I just not get the purpose of this exercise? </p>