std::ranges::views::istream, std::ranges::basic_istream_view, std::ranges::istream_view, std::ranges::wistream_view
定義於標頭檔案 <ranges> |
||
template< std::movable Val, class CharT, class Traits = std::char_traits<CharT> > |
(1) | (C++20 起) |
幫助模板 |
||
template< class Val > using istream_view = ranges::basic_istream_view<Val, char>; |
(2) | (C++20 起) |
template< class Val > using wistream_view = ranges::basic_istream_view<Val, wchar_t>; |
(3) | (C++20 起) |
自定義點物件 |
||
namespace views { template< class T > |
(4) | (C++20 起) |
輔助概念 |
||
template< class Val, class CharT, class Traits > concept /*stream-extractable*/ = |
(5) | (僅作說明*) |
U
是 std::remove_reference_t<decltype(e)>。U
不是從 std::basic_istream<typename U::char_type, typename U::traits_type> 公開且明確派生的,則程式格式錯誤,這可能導致替換失敗。Val
的左值可以從型別為 std::basic_istream<CharT, Traits> 的左值中提取時,僅用於說明的概念 /*stream-extractable*/<Val, CharT, Traits> 得到滿足。basic_istream_view
的迭代器型別是僅可移動的:它不滿足 LegacyIterator 要求,因此不適用於 C++20 之前的演算法。
目錄 |
自定義點物件
名稱 views::istream<T>
表示一個*自定義點物件*,它是一個 字面量 semiregular
類型別的 const 函式物件。為了說明目的,其 cv-unqualified 版本的型別表示為 __istream_fn<T>
。
所有 __istream_fn<T>
的例項都是相等的。在相同引數上呼叫不同 __istream_fn<T>
例項的效果是等價的,無論表示例項的表示式是左值還是右值,以及是否為 const-qualified(但是,volatile-qualified 例項不要求可呼叫)。因此,views::istream<T>
可以自由複製,並且其副本可以互換使用。
給定一組型別 Args...
,如果 std::declval<Args>()... 滿足上述 views::istream<T>
引數的要求,則 __istream_fn<T>
建模
- std::invocable<__istream_fn<T>, Args...>,
- std::invocable<const __istream_fn<T>, Args...>,
- std::invocable<__istream_fn<T>&, Args...>,以及
- std::invocable<const __istream_fn<T>&, Args...>.
否則,__istream_fn<T>
的任何函式呼叫運算子都不參與過載決議。
[編輯] 資料成員
成員 | 定義 |
std::basic_istream<CharT, Traits>* stream_ |
指向輸入流的指標 (僅用於闡釋的成員物件*) |
Val value_ |
儲存的值 (僅用於闡釋的成員物件*) |
[編輯] 成員函式
構造 basic_istream_view (公開成員函式) | |
返回一個迭代器 (公開成員函式) | |
返回 std::default_sentinel (公開成員函式) | |
繼承自 std::ranges::view_interface | |
(C++23) |
返回範圍開頭的常量迭代器 ( std::ranges::view_interface<D> 的公有成員函式) |
(C++23) |
返回範圍常量迭代器的哨兵 ( std::ranges::view_interface<D> 的公有成員函式) |
儘管 |
(直至 C++23) |
std::ranges::basic_istream_view::basic_istream_view
constexpr explicit basic_istream_view( std::basic_istream<CharT, Traits>& stream ); |
(C++20 起) | |
使用 std::addressof(stream) 初始化 stream_
,並對 value_
進行值初始化。
std::ranges::basic_istream_view::begin
constexpr auto begin(); |
(C++20 起) | |
std::ranges::basic_istream_view::end
constexpr std::default_sentinel_t end() const noexcept; |
(C++20 起) | |
[編輯] 巢狀類
basic_istream_view 的迭代器型別(僅用於說明的成員類*) |
[編輯] 示例
#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::views::istream<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::views::istream<float>(floats), std::ostream_iterator<float>{std::cout, ", "} ); std::cout << '\n'; }
輸出
/today/ /is/ /yesterday’s/ /tomorrow/ 1.1, 2.2, 3.3, 4.4, 55, 66, 7.7, 8.8,
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3568 | C++20 | P2325R3 意外地使儲存值預設初始化 | 恢復為值初始化 |
P2325R3 | C++20 | 預設建構函式提供為view 必須是 default_initializable |
與此一同刪除 要求 |
P2432R1 | C++20 | ranges::istream_view 是一個函式模板且不遵循命名約定 |
製作成別名模板; 新增自定義點物件 |
[編輯] 另請參閱
從std::basic_istream讀取的輸入迭代器 (類模板) |