开发者

Operator precedence in `copy` implementation example

开发者 https://www.devze.com 2023-03-23 20:04 出处:网络
I read a few lines of code here where it looks to me like there should be some parentheses. template<class InputIterator, class OutputIterator>

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消