|
| 1 | +--- |
| 2 | +title: "basic_istream_view class (C++ Standard Library)| Microsoft Docs" |
| 3 | +description: "API reference for the Standard Template Library (STL) <ranges> basic_istream_view class, which reads (using operator>>) successive elements from an input stream." |
| 4 | +ms.date: 09/27/2022 |
| 5 | +f1_keywords: ["ranges/std::basic_istream_view", "ranges/std::basic_istream_view::base", "ranges/std::basic_istream_view::begin", "ranges/std::basic_istream_view::data", "ranges/std::basic_istream_view::empty", "ranges/std::basic_istream_view::end", "ranges/std::basic_istream_view::size", "ranges/std::basic_istream_view::operator bool", "ranges/std::basic_istream_view::back", "ranges/std::basic_istream_view::front", "ranges/std::basic_istream_view::operator[]"] |
| 6 | +helpviewer_keywords: ["std::ranges::basic_istream_view [C++]", "std::ranges::basic_istream_view::base [C++]", "std::ranges::basic_istream_view::begin [C++]", "std::ranges::basic_istream_view::data [C++]", "std::ranges::basic_istream_view::empty [C++]", "std::ranges::basic_istream_view::end [C++]", "std::ranges::basic_istream_view::size [C++]", "std::ranges::basic_istream_view::back [C++]", "std::ranges::basic_istream_view::front [C++]", "std::ranges::basic_istream_view::operator[] [C++]", "std::ranges::basic_istream_view::operator bool [C++]"] |
| 7 | +dev_langs: ["C++"] |
| 8 | +--- |
| 9 | +# `basic_istream_view` class (C++ Standard Library) |
| 10 | + |
| 11 | +A view of successive elements from an input stream. |
| 12 | + |
| 13 | +## Syntax |
| 14 | + |
| 15 | +```cpp |
| 16 | +template<movable Val, class CharT, class Traits> |
| 17 | + requires default_initializable<Val> && |
| 18 | + stream_extractable <Val, CharT, Traits> |
| 19 | +class basic_istream_view : public view_interface<basic_istream_view<Val, CharT, Traits>>; |
| 20 | +``` |
| 21 | +
|
| 22 | +### Template parameters |
| 23 | +
|
| 24 | +*`CharT`*\ |
| 25 | +The character type of the stream. |
| 26 | +
|
| 27 | +*`Traits`*\ |
| 28 | +Optional. Provides details about the character type of the stream regarding comparing characters, determining the length of a string made up of that character type, and so on. An example trait is [`char_traits<wchar_t>`](char-traits-wchar-t-struct.md). If not specified, defaults to `char_traits<CharT>`. |
| 29 | +
|
| 30 | +*`Val`*\ |
| 31 | +The type of the elements to extract. For example, `double` given a stream of: `"1.1 2.2 3.3"` |
| 32 | +
|
| 33 | +`stream_extractable` refers to the requirement (concept) that the type `Val` can be extracted from a stream using the `operator>>` function. It's equivalent to: |
| 34 | +
|
| 35 | +```cpp |
| 36 | +template<class Val, class CharT, class Traits> |
| 37 | +concept stream_extractable = |
| 38 | + requires(std::basic_istream<CharT,Traits>& is, Val& t) { |
| 39 | + is >> t; |
| 40 | + }; |
| 41 | +``` |
| 42 | + |
| 43 | +## View characteristics |
| 44 | + |
| 45 | +For a description of the following entries, see [View class characteristics](view-classes.md#view-classes-characteristics) |
| 46 | + |
| 47 | +| Characteristic | Description | |
| 48 | +|--|--| |
| 49 | +| **Range adaptor** | [`istream`](range-adaptors.md#istream) | |
| 50 | +| **Underlying range** | None | |
| 51 | +| **Element type** |The same as `Val` | |
| 52 | +| **View iterator category** | `input_range` | |
| 53 | +| **Sized** | No | |
| 54 | +| **Is `const`-iterable** | No | |
| 55 | +| **Common range** | No | |
| 56 | +| **Borrowed range** | No | |
| 57 | + |
| 58 | +## Specializations |
| 59 | + |
| 60 | +Convenience alias templates are provided for `char` and `wchar_t` streams, as follows: |
| 61 | + |
| 62 | +```cpp |
| 63 | +1) template<class Val> |
| 64 | +using istream_view = ranges::basic_istream_view<Val, char>; |
| 65 | + |
| 66 | +2) template<class Val> |
| 67 | +using wistream_view = ranges::basic_istream_view<Val, wchar_t>; |
| 68 | +``` |
| 69 | + |
| 70 | +1\) Reads elements from an input stream composed of `char` characters.\ |
| 71 | +2\) Reads elements from an input stream composed of `wchar_t` characters. |
| 72 | + |
| 73 | +For 1) and 2), `Val` refers to the type of the elements to extract. For example, `double` given a stream of: `"1.1 2.2 3.3"` |
| 74 | + |
| 75 | +## Members |
| 76 | + |
| 77 | +| **Member functions** | **Description** | |
| 78 | +|--|--| |
| 79 | +| [Constructors](#constructors)<sup>C++20</sup> | Construct a `basic_istream_view`. | |
| 80 | +| [`begin`](#begin)<sup>C++20</sup> | Read the first value and get an iterator for the view. | |
| 81 | +| [`end`](#end)<sup>C++20</sup> | Returns `std::default_sentinel` | |
| 82 | + |
| 83 | +No member functions are inherited from `view_interface`. |
| 84 | + |
| 85 | +## Requirements |
| 86 | + |
| 87 | +**Header:** `<ranges>` (since C++ 20) |
| 88 | + |
| 89 | +**Namespace:** `std::ranges` |
| 90 | + |
| 91 | +**Compiler Option:** [`/std:c++20`](../build/reference/std-specify-language-standard-version.md) or later is required. |
| 92 | + |
| 93 | +## Constructors |
| 94 | + |
| 95 | +Construct an instance of a `basic_istream_view`. |
| 96 | + |
| 97 | +```cpp |
| 98 | +constexpr explicit |
| 99 | +basic_istream_view(std::basic_istream<CharT, Traits>& stream); |
| 100 | +``` |
| 101 | +
|
| 102 | +### Parameters |
| 103 | +
|
| 104 | +*`stream`*\ |
| 105 | +The stream to read from. |
| 106 | +
|
| 107 | +For information about the template parameter type, see [Template parameters](#template-parameters). |
| 108 | +
|
| 109 | +### Return value |
| 110 | +
|
| 111 | +A `basic_istream_view` instance. The `basic_istream_view` internal stream pointer is initialized to `addressof(stream)`. |
| 112 | +
|
| 113 | +### Remarks |
| 114 | +
|
| 115 | +The best way to create a `basic_istream_view` is by using the [`views::istream`](range-adaptors.md#istream) range adaptor. Range adaptors are the intended way to create view classes. The view types are exposed in case you want to create your own custom view type. |
| 116 | +
|
| 117 | +### Example: `basic_istream_view` |
| 118 | +
|
| 119 | +```cpp |
| 120 | +// requires /std:c++20 or later |
| 121 | +#include <ranges> |
| 122 | +#include <iostream> |
| 123 | +#include <sstream> |
| 124 | +
|
| 125 | +int main() |
| 126 | +{ |
| 127 | + // range adaptor |
| 128 | + std::istringstream doubles{ "1.1 2.2 3.3 4.4 5.5" }; |
| 129 | + for (const auto& elem : std::views::istream<double>(doubles)) |
| 130 | + { |
| 131 | + std::cout << elem << ' '; // 1.1 2.2 3.3 4.4 5.5 |
| 132 | + } |
| 133 | + std::cout << '\n'; |
| 134 | +
|
| 135 | + // istream_view alias |
| 136 | + std::istringstream cpu1{ "8 0 8 0" }; |
| 137 | + // equivalent std::ranges::istream_view<int, char> |
| 138 | + for (const auto& elem : std::ranges::istream_view<int>{cpu1}) |
| 139 | + { |
| 140 | + std::cout << elem; // 8080 |
| 141 | + } |
| 142 | + std::cout << '\n'; |
| 143 | + |
| 144 | + // wistream_view alias |
| 145 | + std::wistringstream cpu2{ L"6 5 0 2" }; |
| 146 | + // equivalent std::ranges::istream_view<int, wchar_t> |
| 147 | + for (const auto& elem : std::ranges::wistream_view<int>{cpu2}) |
| 148 | + { |
| 149 | + std::cout << elem; // 6502 |
| 150 | + } |
| 151 | + std::cout << '\n'; |
| 152 | +
|
| 153 | + // specify all template arguments |
| 154 | + std::wistringstream misc(L"S T L"); |
| 155 | + std::ranges::basic_istream_view<wchar_t, wchar_t, std::char_traits<wchar_t>> basic{ misc }; |
| 156 | + for (const auto& elem : basic) |
| 157 | + { |
| 158 | + std::wcout << elem << ' '; // STL |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +```output |
| 164 | +1.1 2.2 3.3 4.4 5.5 |
| 165 | +8080 |
| 166 | +6502 |
| 167 | +S T L |
| 168 | +``` |
| 169 | + |
| 170 | +## `begin` |
| 171 | + |
| 172 | +Read the first value and get an iterator to the view. |
| 173 | + |
| 174 | +```cpp |
| 175 | +constexpr auto begin(); |
| 176 | +``` |
| 177 | + |
| 178 | +### Parameters |
| 179 | + |
| 180 | +None. |
| 181 | + |
| 182 | +### Return value |
| 183 | + |
| 184 | +An iterator pointing at the first element in the `basic_istream_view`. |
| 185 | + |
| 186 | +## `end` |
| 187 | + |
| 188 | +Get the sentinel at the end of the view. |
| 189 | + |
| 190 | +```cpp |
| 191 | +constexpr std::default_sentinel_t end() const noexcept; |
| 192 | +``` |
| 193 | + |
| 194 | +### Parameters |
| 195 | + |
| 196 | +None. |
| 197 | + |
| 198 | +### Return value |
| 199 | + |
| 200 | +`default_sentinel_t` |
| 201 | + |
| 202 | +## See also |
| 203 | + |
| 204 | +[`<ranges>`](ranges.md)\ |
| 205 | +[View classes](view-classes.md) |
0 commit comments