Does the following program perform undefined behavior?
#include <iostream> #include <iterator> int main(int argc, char* argv[]) { for (auto it = std::istream_iterator<std::string>(std::cin); it != std::istream_iterator<std::string>(); ++it) { std::cout << *it << " "; } return 0; }
This 4 year old question says they cannot be compared:
Iterators may also have special values that are not associated with any container. [Example: after declaring an uninitialized pointer x (as for int * x;), x should always be considered that the singular value of the pointer. ] The results of most expressions are undefined for singular values; the only exception is the assignment of a non-singular value to an iterator that has a singular value.
But another answer for the C ++ 14 standard:
However, iterators initialized by initialization can compare and compare equal to other iterators with initialization values of the same type.
.
istream_iterator - , , ++ 14, , . istream_iterator , . , (§24.6.1 [istream.iterator])
istream_iterator
istream_iterator() - , , . [...]. . , .
istream_iterator() - , , . [...]
istream_iterator()
. . , .
( ) ++ 14. , . . - . undefined ++ 14:
std::list<int> l; if(l.begin() == std::list<int>::iterator()) foo(); else bar();