Search code examples
c++variable-assignmentincrementdereferenceoperator-precedence

Operator precedence in `copy` implementation example


I read a few lines of code here where it looks to me like there should be some parentheses.

template<class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
{
  while (first!=last) 
     *result++ = *first++; // <--- this line
  return result;
}

According to the operator precedence table here, I would think that the postfix increment would take precedence, then the dereference, then the assignment. But it looks to me like the intention is that the dereference occurs first, then the assignment, and then the postfix increment.

Am I reading wrong? Or is the table wrong, or the code snippet? Or is there something else to it?


Solution

  • The postfix increment does execute first, but the return value from postfix increment is the original value of the pointer. That's why it works.