9 Iterators library [iterators]

9.8 Stream iterators [iterators.stream]

9.8.3 Class template istreambuf_iterator [istreambuf.iterator]

The class template istreambuf_iterator defines an input iterator ([iterators.input]) that reads successive characters from the streambuf for which it was constructed. operator* provides access to the current input character, if any. Each time operator++ is evaluated, the iterator advances to the next input character. If the end of stream is reached (streambuf_type::sgetc() returns traits::eof()), the iterator becomes equal to the end-of-stream iterator value. The default constructor istreambuf_iterator() and the constructor istreambuf_iterator(nullptr) both construct an end-of-stream iterator object suitable for use as an end-of-range. All specializations of istreambuf_iterator shall have a trivial copy constructor, a constexpr default constructor, and a trivial destructor.

The result of operator*() on an end-of-stream iterator is undefined. For any other iterator value a char_type value is returned. It is impossible to assign a character via an input iterator.

namespace std { namespace experimental { namespace ranges { inline namespace v1 {
  template <class charT, class traits = char_traits<charT>>
  class istreambuf_iterator {
  public:
    typedef input_iterator_tag             iterator_category;
    typedef charT                          value_type;
    typedef typename traits::off_type      difference_type;
    typedef charT                          reference;
    typedef unspecified                   pointer;
    typedef charT                          char_type;
    typedef traits                         traits_type;
    typedef typename traits::int_type      int_type;
    typedef basic_streambuf<charT, traits> streambuf_type;
    typedef basic_istream<charT, traits>   istream_type;

    class proxy;                           // exposition only

    constexpr istreambuf_iterator() noexcept;
    constexpr istreambuf_iterator(default_sentinel) noexcept;
    istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
    ~istreambuf_iterator() = default;
    istreambuf_iterator(istream_type& s) noexcept;
    istreambuf_iterator(streambuf_type* s) noexcept;
    istreambuf_iterator(const proxy& p) noexcept;
    charT operator*() const;
    istreambuf_iterator& operator++();
    proxy operator++(int);
    bool equal(const istreambuf_iterator& b) const;
  private:
    streambuf_type* sbuf_;                // exposition only
  };

  template <class charT, class traits>
    bool operator==(const istreambuf_iterator<charT, traits>& a,
            const istreambuf_iterator<charT, traits>& b);
  template <class charT, class traits>
    bool operator==(default_sentinel a,
            const istreambuf_iterator<charT, traits>& b);
  template <class charT, class traits>
    bool operator==(const istreambuf_iterator<charT, traits>& a,
            default_sentinel b);
  template <class charT, class traits>
    bool operator!=(const istreambuf_iterator<charT, traits>& a,
            const istreambuf_iterator<charT, traits>& b);
  template <class charT, class traits>
    bool operator!=(default_sentinel a,
            const istreambuf_iterator<charT, traits>& b);
  template <class charT, class traits>
    bool operator!=(const istreambuf_iterator<charT, traits>& a,
            default_sentinel b);
}}}}

9.8.3.1 Class template istreambuf_iterator::proxy [istreambuf.iterator::proxy]

namespace std { namespace experimental { namespace ranges { inline namespace v1 {
  template <class charT, class traits = char_traits<charT>>
  class istreambuf_iterator<charT, traits>::proxy { // exposition only
    charT keep_;
    basic_streambuf<charT, traits>* sbuf_;
    proxy(charT c, basic_streambuf<charT, traits>* sbuf)
      : keep_(c), sbuf_(sbuf) { }
  public:
    charT operator*() { return keep_; }
  };
}}}}

Class istreambuf_iterator<charT, traits>::proxy is for exposition only. An implementation is permitted to provide equivalent functionality without providing a class with this name. Class istreambuf_iterator<charT, traits>::proxy provides a temporary placeholder as the return value of the post-increment operator (operator++). It keeps the character pointed to by the previous value of the iterator for some possible future access to get the character.

9.8.3.2 istreambuf_iterator constructors [istreambuf.iterator.cons]

constexpr istreambuf_iterator() noexcept; constexpr istreambuf_iterator(default_sentinel) noexcept;

Effects: Constructs the end-of-stream iterator.

istreambuf_iterator(basic_istream<charT, traits>& s) noexcept; istreambuf_iterator(basic_streambuf<charT, traits>* s) noexcept;

Effects: Constructs an istreambuf_iterator that uses the basic_streambuf object *(s.rdbuf()), or *s, respectively. Constructs an end-of-stream iterator if s.rdbuf() is null.

istreambuf_iterator(const proxy& p) noexcept;

Effects: Constructs a istreambuf_iterator that uses the basic_streambuf object pointed to by the proxy object's constructor argument p.

9.8.3.3 istreambuf_iterator::operator* [istreambuf.iterator::op*]

charT operator*() const

Returns: The character obtained via the streambuf member sbuf_->sgetc().

9.8.3.4 istreambuf_iterator::operator++ [istreambuf.iterator::op++]

istreambuf_iterator& istreambuf_iterator<charT, traits>::operator++();

Effects: Equivalent to sbuf_->sbumpc().

Returns: *this.

proxy istreambuf_iterator<charT, traits>::operator++(int);

Effects: Equivalent to: return proxy(sbuf_->sbumpc(), sbuf_);

9.8.3.5 istreambuf_iterator::equal [istreambuf.iterator::equal]

bool equal(const istreambuf_iterator& b) const;

Returns: true if and only if both iterators are at end-of-stream, or neither is at end-of-stream, regardless of what streambuf object they use.

9.8.3.6 operator== [istreambuf.iterator::op==]

template <class charT, class traits> bool operator==(const istreambuf_iterator<charT, traits>& a, const istreambuf_iterator<charT, traits>& b);

Effects: Equivalent to: return a.equal(b);

template <class charT, class traits> bool operator==(default_sentinel a, const istreambuf_iterator<charT, traits>& b);

Effects: Equivalent to: return istreambuf_iterator<charT, traits>{}.equal(b);

template <class charT, class traits> bool operator==(const istreambuf_iterator<charT, traits>& a, default_sentinel b);

Effects: Equivalent to: return a.equal(istreambuf_iterator<charT, traits>{});

9.8.3.7 operator!= [istreambuf.iterator::op!=]

template <class charT, class traits> bool operator!=(const istreambuf_iterator<charT, traits>& a, const istreambuf_iterator<charT, traits>& b); template <class charT, class traits> bool operator!=(default_sentinel a, const istreambuf_iterator<charT, traits>& b); template <class charT, class traits> bool operator!=(const istreambuf_iterator<charT, traits>& a, default_sentinel b);

Effects: Equivalent to: return !(a == b);