std::ranges::basic_istream_view, std::ranges::istream_view

< cpp‎ | ranges
Defined in header <ranges>
template< std::movable Val, class CharT, class Traits = std::char_traits<CharT> >

  requires std::default_initializable<Val> &&

class basic_istream_view : public ranges::view_interface<basic_istream_view<Val,CharT,Traits>>
(1) (since C++20)
template< class Val, class CharT, class Traits >
basic_istream_view<Val,CharT,Traits> istream_view( std::basic_istream<CharT,Traits>& s );
(2) (since C++20)
1) A range factory that generates a sequence of elements by repeatedly calling operator>>.
2) Returns a basic_istream_view, deducing CharT and Traits from s. Equivalent to return basic_istream_view<Val, CharT, Traits>{s}.

The exposition-only concept /*stream-extractable*/<Val,CharT,Traits> is satisfied when lvalue of Val can be extracted from lvalue of std::basic_istream<CharT,Traits>. Its definition is equivalent to

template< class Val, class CharT, class Traits >

concept /*stream-extractable*/ =
  requires(std::basic_istream<CharT,Traits>& is, Val& t) {
    is >> t;


The iterator type of basic_istream_view is move-only: it does not meet the LegacyIterator requirements, and thus does not work with pre-C++20 algorithms. Furthermore, because its std::iterator_traits does not provide a iterator_category, it does not work with standard iterator adaptors.


[edit] Member functions

constructs a basic_istream_view
(public member function)
returns an iterator
(public member function)
returns std::default_sentinel
(public member function)


basic_istream_view() = default;
constexpr explicit basic_istream_view( std::basic_istream<CharT, Traits>& stream );
1) Default constructor. Value-initializes the stored pointer to the stream and the stored value of Val. Use of an iterator to default constructed istream view is undefined behavior.
2) Initializes the stored pointer to stream with std::addressof(stream), and value-initializes the stored value of Val.


constexpr auto begin();

Equivalent to:

if (stream_) {
    *stream_ >> value_;
return /*iterator*/{*this};

where stream_ is the stored pointer to stream and value_ is the stored value of Val.


constexpr std::default_sentinel_t end() const noexcept;

Equivalent to return std::default_sentinel;.

[edit] Nested classes


struct /*iterator*/;

The return type of basic_istream_view::begin.

This is an input_iterator.

Member types

Member type Definition
iterator_concept std::input_iterator_tag
difference_type std::ptrdiff_t
value_type Val

Member functions

constructs an iterator
(public member function)
the copy assignment operator is deleted
(public member function)
advances or decrements the iterator
(public member function)
returns the current element
(public member function)

Non-member functions

compares with a std::default_sentinel_t
(public member function)


/*iterator*/() = default;
constexpr explicit /*iterator*/( basic_istream_view& parent );
/*iterator*/( const /*iterator*/& ) = delete;
/*iterator*/( /*iterator*/&& ) = default;
1) Default constructor. Constructs an end iterator that compares equal to std::default_sentinel.
2) Constructs an iterator from the parent basic_istream_view.
3) The copy constructor is deleted. The iterator is not copyable.
4) The move constructor is defaulted. The iterator is movable.


/*iterator*/& operator=( const /*iterator*/& ) = delete;
/*iterator*/& operator=( /*iterator*/&& ) = default;
3) The copy assignment operator is deleted. The iterator is not copyable.
4) The move assignment operator is defaulted. The iterator is movable.


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

Reads a value from the underlying stream and stores it into the parent basic_istream_view.

Return value

1) *this
2) (none)


Val& operator*() const;

Returns a reference to the stored value.

operator==(std::ranges::basic_istream_view::iterator, std::default_sentinel)

friend bool operator==( const /*iterator*/& x, std::default_sentinel_t );

Compares an iterator with std::default_sentinel_t.

Returns true if *this does not have a parent basic_istream_view, or if an error has occurred on the underlying stream.

This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::ranges::basic_istream_view::iterator is an associated class of the arguments.

[edit] Example

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <ranges>
#include <sstream>
#include <string>
int main()
    auto words = std::istringstream{"today is yesterday's tomorrow"};
    for (const auto s: std::ranges::istream_view<std::string>(words)) {
        std::cout << std::quoted(s) << ' ';
    std::cout << '\n';
    auto floats = std::istringstream{"1.1  2.2\t3.3\v4.4\f55\n66\r7.7  8.8"};
        std::ostream_iterator<float>{std::cout, ", "});
    std::cout << '\n';


"today" "is" "yesterday's" "tomorrow"
1.1, 2.2, 3.3, 4.4, 55, 66, 7.7, 8.8,

[edit] See also

input iterator that reads from std::basic_istream
(class template) [edit]