Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | ranges
 
 
 
std::ranges::basic_istream_view
 
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 >

ranges::basic_istream_view<Val,CharT,Traits>

    istream_view( std::basic_istream<CharT,Traits>& s );
(2) (since C++20)
Helper concepts
template< class Val, class CharT, class Traits >

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

  };
(3) (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};.
3) 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>.

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.

Although basic_istream_view is derived from std::ranges::view_interface, it cannot use any of inherited member functions.

Contents

[edit] Member functions

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

std::ranges::basic_istream_view::basic_istream_view

basic_istream_view() = default;
(1) (since C++20)
constexpr explicit
    basic_istream_view( std::basic_istream<CharT, Traits>& stream );
(2) (since C++20)
1) Default constructor. Value-initializes the stored pointer to the stream and the stored value of Val. Use of an iterator to default constructed basic_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();
(since C++20)

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;
(since C++20)

Equivalent to return std::default_sentinel;.

[edit] Nested classes

(C++20)
the iterator type of basic_istream_view, the name is exposition-only
(class)

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