23 Iterators library [iterators]

23.6 Stream iterators [stream.iterators]

23.6.1 Class template istream_­iterator [istream.iterator]

The class template istream_­iterator is an input iterator ([input.iterators]) that reads successive elements from the input stream for which it was constructed.
namespace std {
  template<class T, class charT = char, class traits = char_traits<charT>,
           class Distance = ptrdiff_t>
  class istream_iterator {
  public:
    using iterator_category = input_iterator_tag;
    using value_type        = T;
    using difference_type   = Distance;
    using pointer           = const T*;
    using reference         = const T&;
    using char_type         = charT;
    using traits_type       = traits;
    using istream_type      = basic_istream<charT,traits>;

    constexpr istream_iterator();
    constexpr istream_iterator(default_sentinel_t);
    istream_iterator(istream_type& s);
    istream_iterator(const istream_iterator& x) = default;
    ~istream_iterator() = default;
    istream_iterator& operator=(const istream_iterator&) = default;

    const T& operator*() const;
    const T* operator->() const;
    istream_iterator& operator++();
    istream_iterator  operator++(int);

    friend bool operator==(const istream_iterator& i, default_sentinel_t);
    friend bool operator==(default_sentinel_t, const istream_iterator& i);
    friend bool operator!=(const istream_iterator& x, default_sentinel_t y);
    friend bool operator!=(default_sentinel_t x, const istream_iterator& y);

  private:
    basic_istream<charT,traits>* in_stream; // exposition only
    T value;                                // exposition only
  };
}
The type T shall meet the Cpp17DefaultConstructible, Cpp17CopyConstructible, and Cpp17CopyAssignable requirements.

23.6.1.1 Constructors and destructor [istream.iterator.cons]

constexpr istream_iterator(); constexpr istream_iterator(default_sentinel_t);
Effects: Constructs the end-of-stream iterator, value-initializing value.
Ensures: in_­stream == nullptr is true.
Remarks: If the initializer T() in the declaration auto x = T(); is a constant initializer ([expr.const]), then these constructors are constexpr constructors.
istream_iterator(istream_type& s);
Effects: Initializes in_­stream with addressof(s), value-initializes value, and then calls operator++().
istream_iterator(const istream_iterator& x) = default;
Ensures: in_­stream == x.in_­stream is true.
Remarks: If is_­trivially_­copy_­constructible_­v<T> is true, then this constructor is trivial.
~istream_iterator() = default;
Remarks: If is_­trivially_­destructible_­v<T> is true, then this destructor is trivial.

23.6.1.2 Operations [istream.iterator.ops]

const T& operator*() const;
Expects: in_­stream != nullptr is true.
Returns: value.
const T* operator->() const;
Expects: in_­stream != nullptr is true.
Returns: addressof(value).
istream_iterator& operator++();
Expects: in_­stream != nullptr is true.
Effects: Equivalent to:
if (!(*in_stream >> value))
  in_stream = nullptr;
Returns: *this.
istream_iterator operator++(int);
Expects: in_­stream != nullptr is true.
Effects: Equivalent to:
istream_iterator tmp = *this;
++*this;
return tmp;
template<class T, class charT, class traits, class Distance> bool operator==(const istream_iterator<T,charT,traits,Distance>& x, const istream_iterator<T,charT,traits,Distance>& y);
Returns: x.in_­stream == y.in_­stream.
friend bool operator==(default_sentinel_t, const istream_iterator& i); friend bool operator==(const istream_iterator& i, default_sentinel_t);
Returns: !i.in_­stream.
template<class T, class charT, class traits, class Distance> bool operator!=(const istream_iterator<T,charT,traits,Distance>& x, const istream_iterator<T,charT,traits,Distance>& y); friend bool operator!=(default_sentinel_t x, const istream_iterator& y); friend bool operator!=(const istream_iterator& x, default_sentinel_t y);
Returns: !(x == y)