Search code examples
c++operatorspostfix-operator

Stream insertion combined with postfix operators


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--;

Solution

  • The result of a post-increment operator is a temporary object, even if the increment is strictly applied only at the end of the expression. To read a value, an lvalue is needed but temporary objects aren't lvalues: they are bound to go away. Note, that you'd also read the value into the temporary, retaining an unspecified value in a.