An std::istream_view<T>
is a range; and more specifically, a range formed as a view. This addition to the standard library is akin to what you might find under std::ranges::views
- except that it's not a view of an arbitrary range, but of an std::istream
.
So what "viewing" is applied to an std::istream
? Recall an istream
is a stream of characters, not of arbitrary T
-type elements of your choice. The lazy application of parsing those characters into consecutive T
's is the "viewing" of the the istream. That is, the k'th element of std::istream_view<T>(is)
is what you'd get the k'th time running is >> t
for t
of type T
.
You would use an std::istream_view
(carefully) when you want to apply your code, which works with ranges, directly to input data - rather than first parsing your input into some data structure in a more "old-school" manner, then working on that structure as a range.
Other takes on what an std::istream_view
is:
- @Barry has described it as the equivalent of a coroutine which parses
T
s from an istream; read this answer for details (note it's about the istream view in the ranges-v3, much of which became the standard ranges library).
- @NicolBolas considers it to be the result of "wrapping
std::istream_iterator<T>
in a C++20 view interface".