Apparently, C++20 has a new std::istream
-related construct: std::istream_view
. The cppreference page on it is a stub right now†. So, what is a "view of an istream
" and what can I use it for?
† - Ok, technically it redirects to a page about std::basic_istream_view
and that one's a stub.
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:
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).std::istream_iterator<T>
in a C++20 view interface".