Namespaces
Variants
Views
Actions

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

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

  requires std::default_initializable<Val> &&
           /*stream-extractable*/<Val,CharT,Traits>

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.

Contents

[edit] Member functions

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

std::ranges::basic_istream_view::basic_istream_view

basic_istream_view() = default;
(1)
constexpr explicit basic_istream_view( std::basic_istream<CharT, Traits>& stream );
(2)
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.

std::ranges::basic_istream_view::begin

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.

std::ranges::basic_istream_view::end

constexpr std::default_sentinel_t end() const noexcept;

Equivalent to return std::default_sentinel;.

[edit] Nested classes

std::ranges::basic_istream_view::iterator

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

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

Non-member functions

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

std::ranges::basic_istream_view::iterator::iterator

/*iterator*/() = default;
(1)
constexpr explicit /*iterator*/( basic_istream_view& parent );
(2)
/*iterator*/( const /*iterator*/& ) = delete;
(3)
/*iterator*/( /*iterator*/&& ) = default;
(4)
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.

std::ranges::basic_istream_view::iterator::operator=

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

std::ranges::basic_istream_view::iterator::operator++

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

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

Return value

1) *this
2) (none)

std::ranges::basic_istream_view::iterator::operator*

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::ranges::copy(
        std::ranges::istream_view<float>(floats),
        std::ostream_iterator<float>{std::cout, ", "});
    std::cout << '\n';
}

Output:

"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]