1

Say I was reading data in which the indexing starts at 1. I want my indexing to be 0 based.

Why is it not acceptable to use postfix increment/decrement operators after a stream insertion operator?

int a;
std::cin >> a--;
4

2 回答 2

4

后自增运算符的结果是一个临时对象,即使仅在表达式末尾严格应用自增。要读取一个值,需要一个左值,但临时对象不是左值:它们必然会消失。请注意,您还将将该值读入临时文件,在a.

于 2014-11-07T19:59:34.347 回答
1

operator>>有一个需要 a 的重载int&。的结果a--是右值,您不能将非常量引用绑定到右值。

于 2014-11-07T20:09:28.260 回答