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 understd::ranges::views
- except that it's not a view of an arbitrary range, but of anstd::istream
.So what "viewing" is applied to an
std::istream
? Recall anistream
is a stream of characters, not of arbitraryT
-type elements of your choice. The lazy application of parsing those characters into consecutiveT
's is the "viewing" of the the istream. That is, the k'th element ofstd::istream_view<T>(is)
is what you'd get the k'th time runningis >> t
fort
of typeT
.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".