Is it legal to use the increment operator in a C++ function call?

前端 未结 8 1378
时光取名叫无心
时光取名叫无心 2020-11-30 01:56

There\'s been some debate going on in this question about whether the following code is legal C++:

std::list::iterator i = items.begin();
while          


        
8条回答
  •  迷失自我
    2020-11-30 02:30

    Sutter's Guru of the Week #55 (and the corresponding piece in "More Exceptional C++") discusses this exact case as an example.

    According to him, it is perfectly valid code, and in fact a case where trying to transform the statement into two lines:

    items.erase(i);
    i++;
    

    does not produce code that is semantically equivalent to the original statement.

提交回复
热议问题