- streambuf[meta header]
- std[meta namespace]
- basic_streambuf[meta class]
- function[meta id-type]
namespace std {
template<class CharT, class Traits = char_traits<CharT>>
class basic_streambuf {
protected:
virtual streamsize showmanyc();
……
};
}- streamsize[link /reference/ios/type-streamsize.md]
ブロックせずに読み取れると期待される文字数を得る。
デフォルトの動作は 0 を返すのみ。(入力部分列以外の領域がある場合、この関数をオーバーライドすることで、入力部分列以外の領域のサイズを返すようにする。)
0。
#include <iostream>
#include <streambuf>
// streambufを継承したクラス
class dummy_buf : public std::streambuf {
char space_[10] = {};
public:
dummy_buf(void) {
// 入力列に配列を設定
setg(space_, space_, space_ + 2);
}
std::streamsize showmanyc(void) override {
std::cout << "showmanyc" << std::endl;
// ベースクラスのshowmanyc()を呼ぶ
return std::streambuf::showmanyc();
}
};
int main() {
dummy_buf buf{};
std::cout << buf.in_avail() << std::endl; // 残2文字
buf.sbumpc(); // 1文字進める
buf.sbumpc(); // 1文字進める
std::cout << buf.in_avail() << std::endl; // 残0文字。in_avail()からshowmanyc()が呼ばれる
}- std::streambuf::showmanyc[color ff0000]
2
showmanyc
0
- C++98