I’ve got this code snippet. The istream_iterator object is only defined and not used, so I expect that it won’t do anything and application finish immediately. But when I run application, it will not finish before I provide some input. Why?
I’m compiling it on ArchLinux with: gcc 4.7.1, with command: g++ -std=c++11 filename.cpp
#include <iterator>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
istream_iterator<char> input(cin);
return 0;
}
Per the standard,
So it is unspecified whether this program will wait for input.
However, it’s difficult to see how
istream_iteratorcould be implemented otherwise; per 24.6.1:1, after it is constructed […] the iterator reads and stores a value ofT, so if the read does not occur on construction then it would need to occur onoperator *() constand on the freeoperator==(const istream_iterator<T> &, const istream_iterator<T> &), so all of the internal state of the iterator would have to bemutable.