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,
24.6.1.1 istream_iterator constructors and destructor [istream.iterator.cons]
istream_iterator(istream_type& s);
3 - Effects: Initializes in-stream with
&s
. value may be initialized during construction or the first time it is referenced.
So it is unspecified whether this program will wait for input.
However, it's difficult to see how istream_iterator
could be implemented otherwise; per 24.6.1:1, after it is constructed [...] the iterator reads and stores a value of T
, so if the read does not occur on construction then it would need to occur on operator *() const
and on the free operator==(const istream_iterator<T> &, const istream_iterator<T> &)
, so all of the internal state of the iterator would have to be mutable
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With