std::basic_stringbuf<CharT,Traits,Allocator>::underflow
From cppreference.com
< cpp | io | basic stringbuf
| protected: virtual int_type underflow() |
||
バッファの読み取り領域から次の文字を読み取ります。
具体的には、
1) 入力シーケンスに読み取り位置が利用可能な場合(egptr() > gptr())、Traits::to_int_type(*gptr()) を返します。
2) そうでない場合、pptr() > egptr()(直近の overflow() が egptr() を変更して以降、ストリームにいくつかの文字が挿入された)場合、egptr() を pptr() と等しく変更することで読み取り領域の末尾を拡張し、その後 Traits::to_int_type(*gptr()) を返します。
3) それ以外の場合、Traits::eof() を返します。
バッファ内の、コンストラクタで渡された文字列に由来するものか overflow() によって追加されたものかにかかわらず、初期化されたすべての文字は入力シーケンスの一部とみなされます。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
成功した場合は(読み取り領域の次の文字)Traits::to_int_type(*gptr())、失敗した場合は Traits::eof()。
[編集] 例
このコードを実行
#include <iostream> #include <sstream> struct mybuf : std::stringbuf { mybuf(const std::string& new_str, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) : std::stringbuf(new_str, which) {} int_type overflow(int_type c) { std::cout << "Before overflow(): get area size is " << egptr() - eback() << ", the put area size is " << epptr() - pbase() << '\n'; int_type rc = std::stringbuf::overflow(c); std::cout << "After overflow(): get area size is " << egptr() - eback() << ", put area size is " << epptr() - pbase() << '\n'; return rc; } int_type underflow() { std::cout << "Before underflow(): get area size is " << egptr() - eback() << ", put area size is " << epptr() - pbase() << '\n'; int_type ch = std::stringbuf::underflow(); std::cout << "After underflow(): get area size is " << egptr() - eback() << ", put area size is " << epptr() - pbase() << '\n'; if (ch == EOF) std::cout << "underflow() returns EOF\n"; else std::cout << "underflow() returns '" << char(ch) << "'\n"; return ch; } }; int main() { mybuf sbuf("123"); // read-write stream std::iostream stream(&sbuf); int n; stream >> n; // calls sgetc() four times // three calls return the characters '1', '2', '3' // the fourth call, gptr() == egptr() and underflow() is called // underflow returns EOF std::cout << "n = " << n << '\n'; stream.clear(); // clear the eofbit stream << "123456"; // sputc() is called 6 times // first three calls store "123" in the existing buffer // 4th call finds that pptr() == epptr() and calls overflow() // overflow() grows the buffer and sets egptr() to 4 // 5th and 6th calls store '5' and '6', advancing pptr() stream >> n; // calls sgetc() 4 times // 1st call returns the '4' that was made available by overflow() // on the 2nd call, egptr() == egptr() and underflow() is called // underflow advances egptr() to equal pptr() (which is 6) // 3rd sgetc() returns '6' // 4th sgetc() finds gptr() == egptr(), calls underflow() // underflow() returns EOF std::cout << "n = " << n << '\n'; }
実行結果の例
Before underflow(): get area size is 3, put area size is 15 After underflow(): get area size is 3, put area size is 15 underflow() returns EOF n = 123 Before underflow(): get area size is 3, put area size is 15 After underflow(): get area size is 6, put area size is 15 underflow() returns '4' Before underflow(): get area size is 6, put area size is 15 After underflow(): get area size is 6, put area size is 15 underflow() returns EOF n = 456
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 432 | C++98 | overflow() によって追加された文字が入力シーケンスの一部とみなされるかどうかは不明でした。 入力シーケンスの一部とみなされるかどうか |
明確化された |
[編集] 関連項目
| [virtual] |
関連付けられた入力シーケンスから取得領域に文字を読み込む ( std::basic_streambuf<CharT,Traits> の仮想 protected メンバ関数) |
| [virtual] |
関連付けられたファイルから読み込みます。 ( std::basic_filebuf<CharT,Traits>の仮想保護メンバー関数) |
| [virtual] |
入力シーケンスから文字を読み取りますが、次のポインタは進めません。 ( std::strstreambufの仮想保護メンバー関数) |
| シーケンスを進めずに、入力シーケンスから1文字を読み取る ( std::basic_streambuf<CharT,Traits> の public メンバ関数) |