Table 72 in C++03 about input iterator requirements says that the pre-condition of ++r
is that r
is de-referenceable. The same pre-conditions holds for r++
.
Now, 24.5.1/1
says about istream_iterator
The result of operator*
on an end of stream is not defined.
In conclusion, the effects of operator++
on an end-of-stream iterator are undefined.
Table 72 in C++03 about input iterator requirements says that the pre-condition of ++r
is that r
is de-referenceable. The same pre-conditions holds for r++
.
Now, 24.5.1/1
says about istream_iterator
The result of operator*
on an end of stream is not defined.
In conclusion, the effects of operator++
on an end-of-stream iterator are undefined.
Note that I think this conclusion makes behavior undefined only when you write or use an algorithm taking input iterators which exhibits that behavior, and then pass an istream iterator. From only using the istream iterator itself explicitly, without treating it as an input iterator and relying on its invariants, then i think the conclusion above isn't quite right (we may have a class that doesn't require that r
is dereferenceable, for example).
But looking at how istream iterator is described, an invocation of operator++
after reaching the end of stream value results in undefined behavior either. operator==
for it is defined as being equivalent to
x.in_stream == y.in_stream
Where in_stream
is a pointer to the stream iterated over - and exposed into the Standard text for defining behavior and semantics "exposition only". Now, the only implementation i can think of that makes this work, is using an end-of-stream iterator that stores as stream pointer a null pointer. But operator++
is defined as doing something having the effect of the following
*in_stream >>value
Now, if you enter the end-of-stream state, and we would set in_stream
to a null pointer, then surely the effect of that would be undefined behavior.
So even if you use the istream iterator alone, there doesn't seem to be any guarantee that you may increment past the end-of-stream value.