std::istreambuf_iterator
| ヘッダ <iterator> で定義 |
||
| template< class CharT, class Traits = std::char_traits<CharT> > class istreambuf_iterator |
(C++17まで) | |
| template< class CharT, class Traits = std::char_traits<CharT> > class istreambuf_iterator; |
(C++17以降) | |
std::istreambuf_iteratorは、構築されたstd::basic_streambufオブジェクトから連続する文字を読み取る、シングルパス入力イテレータです。
デフォルトコンストラクタで構築されたstd::istreambuf_iteratorは、*ストリーム終了*イテレータとして知られています。std::istreambuf_iteratorが基になるストリームの終わりに達すると、ストリーム終了イテレータと等しくなります。それをさらに逆参照またはインクリメントすると、未定義の動作が発生します。
|
|
(C++11以降) |
目次 |
[編集] メンバ型
| メンバ型 | 定義 |
iterator_category
|
std::input_iterator_tag |
value_type
|
CharT |
difference_type
|
typename Traits::off_type |
pointer
|
/* 不明 */ |
reference
|
CharT |
char_type
|
CharT
|
traits_type
|
Traits
|
int_type
|
typename Traits::int_type |
streambuf_type
|
std::basic_streambuf<CharT, Traits> |
istream_type
|
std::basic_istream<CharT, Traits> |
/* プロキシ */
|
実装定義のクラス型。proxyオブジェクトは、char_type文字とstreambuf_type*ポインタを保持します。operator*でproxyオブジェクトを逆参照すると、保持されている文字が得られます。(説明専用メンバ型*) |
|
メンバ型 |
(C++17まで) |
メンバ型pointerは通常CharT*です(以下を参照)。
[編集] メンバ関数
新しいistreambuf_iteratorを構築します(public member function) | |
| (デストラクタ) (暗黙的に宣言) |
istreambuf_iteratorを破棄します(public member function) |
| 現在の文字のコピーを取得します (public member function) | |
| イテレータを進める (public member function) | |
両方のistreambuf_iteratorがストリーム終了であるか、または両方が有効であるかをテストします(public member function) |
[編集] 非メンバ関数
| (C++20で削除) |
2つのistreambuf_iteratorを比較します(function template) |
[編集] 注記
LWG issue 659の解決により、operator->が導入されました。std::istreambuf_iterator iが与えられた場合、式 (*i).m と i->m は同じ効果を持つと予想されます。
しかし、その解決策は動作の正式な仕様を提供していません。そのため、nullptrを返したり、一時オブジェクトのアドレスを返したり、あるいはメンバを提供しなかったりするなど、実装が異なります。その意図された動作は達成が困難であり、LWG issue 2790の解決により削除されました。
LWG issue 659の解決により、pointerメンバ型は、operator->がプロキシを返すことを可能にするために、不明とされました。これは、CharTがクラス型ではない場合にoperator->がコンパイルできるようにするためです。
[編集] 例
#include <iostream> #include <iterator> #include <sstream> #include <string> int main() { // typical use case: an input stream represented as a pair of iterators std::istringstream in{"Hello, world"}; std::istreambuf_iterator<char> it{in}, end; std::string ss{it, end}; std::cout << "ss has " << ss.size() << " bytes; " "it holds \"" << ss << "\"\n"; // demonstration of the single-pass nature std::istringstream s{"abc"}; std::istreambuf_iterator<char> i1{s}, i2{s}; std::cout << "i1 returns '" << *i1 << "'\n" "i2 returns '" << *i2 << "'\n"; ++i1; std::cout << "after incrementing i1, but not i2:\n" "i1 returns '" << *i1 << "'\n" "i2 returns '" << *i2 << "'\n"; ++i2; std::cout << "after incrementing i2, but not i1:\n" "i1 returns '" << *i1 << "'\n" "i2 returns '" << *i2 << "'\n"; }
出力
ss has 12 bytes; it holds "Hello, world" i1 returns 'a' i2 returns 'a' after incrementing i1, but not i2: i1 returns 'b' i2 returns 'b' after incrementing i2, but not i1: i1 returns 'c' i2 returns 'c'
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 659 | C++98 | 1. std::istreambuf_iteratorにはoperator->がありませんでした。2. メンバ型 pointerはCharT*として指定されていました。 |
1. 追加された 2. 不明に変更されました。 |
| LWG 2790 | C++98 | LWG issue 659によって追加されたoperator->は有用ではありませんでした。 | 削除 |
[編集] 関連項目
| std::basic_streambuf に書き込む出力イテレータ (クラステンプレート) | |
| std::basic_istream から読み取る入力イテレータ (クラステンプレート) |