I am reading chapter 8 of the "Accelerated C++" book. Section 8.3 is about input and output iterators:
vector<int> v; // read ints from the standard input and append them to v copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(v));
[...]
The second argument to copy creates a default (empty) istream_iterator, which is not bound to any file. The istream_iterator type has a default value with the property that any istream_iterator that has reached end-of-file or is in an error state will appear to be equal to the default value. Therefore, we can use the default value to indicate the "one-past-the-end" convention for copy.
This is what I understand: istream_iterator is a template class, and istream_iterator< int> is an instance of the template. Writing istream_iterator< int>() triggers value-initialization of the istream_iterator< int> object, which means zero-initialization + call to implicit default constructor (http://en.cppreference.com/w/cpp/language/value_initialization). I thought that default-initialization of the istream_iterator< int> object would work as well (triggers call to default constructor), so I tried this:
vector<int> v; // read ints from the standard input and append them to v copy(istream_iterator<int>(cin), istream_iterator<int>, back_inserter(v));
But this does not compile:
error: expected primary-expression before ‘,’ token
I don't understand what is going on. Any explanation is welcome.
There's no way to default-initialise, rather than value-initialise, a temporary. While the expression
type()
creates a value-initialised temporary, a type name alone is not a valid expression.However, for any type (like this one) that declares a default constructor, default-initialisation and value-initialisation are equivalent; there is no zero-initialisation before a non-implicit constructor is called.