24 Ranges library [ranges]

24.6 Range factories [range.factories]

24.6.4 Istream view [range.istream]

24.6.4.1 Overview [range.istream.overview]

basic_­istream_­view models input_­range and reads (using operator>>) successive elements from its corresponding input stream.
Example
:
auto ints = istringstream{"0 1  2   3     4"};
ranges::copy(istream_view<int>(ints), ostream_iterator<int>{cout, "-"});
// prints 0-1-2-3-4-
— end example
 ]

24.6.4.2 Class template basic_­istream_­view [range.istream.view]

namespace std::ranges {
  template<class Val, class CharT, class Traits>
    concept stream-extractable =                // exposition only
      requires(basic_istream<CharT, Traits>& is, Val& t) {
         is >> t;
      };

  template<movable Val, class CharT, class Traits>
    requires default_constructible<Val> &&
      stream-extractable<Val, CharT, Traits>
  class basic_istream_view : public view_interface<basic_istream_view<Val, CharT, Traits>> {
  public:
    basic_istream_view() = default;
    constexpr explicit basic_istream_view(basic_istream<CharT, Traits>& stream);

    constexpr auto begin()
    {
      if (stream_) {
        *stream_ >> object_;
      }
      return iterator{*this};
    }

    constexpr default_sentinel_t end() const noexcept;

  private:
    struct iterator;                                    // exposition only
    basic_istream<CharT, Traits>* stream_{};            // exposition only
    Val object_ = Val();                                // exposition only
  };
}
constexpr explicit basic_istream_view(basic_istream<CharT, Traits>& stream);
Effects: Initializes stream_­ with addressof(stream).
constexpr default_sentinel_t end() const noexcept;
Effects: Equivalent to: return default_­sentinel;
template<class Val, class CharT, class Traits> basic_istream_view<Val, CharT, Traits> istream_view(basic_istream<CharT, Traits>& s);
Effects: Equivalent to: return basic_­istream_­view<Val, CharT, Traits>{s};

24.6.4.3 Class template basic_­istream_­view​::​iterator [range.istream.iterator]

namespace std::ranges {
  template<class Val, class CharT, class Traits>
  class basic_istream_view<Val, CharT, Traits>::iterator {      // exposition only
  public:
    using iterator_category = input_iterator_tag;
    using difference_type = ptrdiff_t;
    using value_type = Val;

    iterator() = default;
    constexpr explicit iterator(basic_istream_view& parent) noexcept;

    iterator(const iterator&) = delete;
    iterator(iterator&&) = default;

    iterator& operator=(const iterator&) = delete;
    iterator& operator=(iterator&&) = default;

    iterator& operator++();
    void operator++(int);

    Val& operator*() const;

    friend bool operator==(const iterator& x, default_sentinel_t);

  private:
    basic_istream_view* parent_{};                              // exposition only
  };
}
constexpr explicit iterator(basic_istream_view& parent) noexcept;
Effects: Initializes parent_­ with addressof(parent).
iterator& operator++();
Preconditions: parent_­->stream_­ != nullptr is true.
Effects: Equivalent to:
*parent_->stream >> parent_->object_;
return *this;
void operator++(int);
Preconditions: parent_­->stream_­ != nullptr is true.
Effects: Equivalent to ++*this.
Val& operator*() const;
Preconditions: parent_­->stream_­ != nullptr is true.
Effects: Equivalent to: return parent_­->value_­;
friend bool operator==(const iterator& x, default_sentinel_t);
Effects: Equivalent to: return x.parent_­ == nullptr || !*x.parent_­->stream_­;