Namespaces
Variants

std::strstreambuf:: underflow

From cppreference.net
protected :
virtual int_type underflow ( ) ;
(C++98で非推奨)
(C++26で削除)

バッファの取得領域から次の文字を読み取ります。

入力シーケンスに読み取り位置が利用可能な場合( gptr ( ) < egptr ( ) )、 ( unsigned char ) ( * gptr ( ) ) を返します。

そうでなければ、 pptr() がnullではなく、かつ pptr ( ) > egptr ( ) (put領域が存在し、かつget領域よりも後方に位置している)場合、put領域に最近書き込まれた文字を含めるようにget領域の終端を拡張します。これは egptr() gptr ( ) から pptr() の間の値にインクリメントすることで行われ、その後 ( unsigned char ) ( * gptr ( ) ) を返します。

それ以外の場合、失敗を示すために EOF を返します。

目次

パラメータ

(なし)

戻り値

ゲットエリアの次の文字、 ( unsigned char ) ( * gptr ( ) ) 成功時、 EOF 失敗時。

#include <iostream>
#include <strstream>
struct mybuf : std::strstreambuf
{
    int_type overflow(int_type c) 
    {
        std::cout << "Before overflow(): size of the get area is " << egptr()-eback()
                  << " size of the put area is " << epptr()-pbase() << '\n';
        int_type rc = std::strstreambuf::overflow(c);
        std::cout << "After overflow(): size of the get area is " << egptr()-eback()
                  << " size of the put area is " << epptr()-pbase() << '\n';
        return rc;
    }
    int_type underflow() 
    {
        std::cout << "Before underflow(): size of the get area is " << egptr()-eback()
                  << " size of the put area is " << epptr()-pbase() << '\n';
        int_type ch = std::strstreambuf::underflow();
        std::cout << "After underflow(): size of the get area is " << egptr()-eback()
                  << " size of the put area 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; // read-write dynamic strstreambuf
    std::iostream stream(&sbuf);
    int n;
    stream >> n;
    stream.clear();
    stream << "123";
    stream >> n;
    std::cout << n << '\n';
}

出力例:

Before underflow(): size of the get area is 0 size of the put area is 0
After underflow(): size of the get area is 0 size of the put area is 0
underflow() returns EOF
Before overflow(): size of the get area is 0 size of the put area is 0
After overflow(): size of the get area is 0 size of the put area is 32
Before underflow(): size of the get area is 0 size of the put area is 32
After underflow(): size of the get area is 3 size of the put area is 32
underflow() returns '1'
Before underflow(): size of the get area is 3 size of the put area is 32
After underflow(): size of the get area is 3 size of the put area is 32
underflow() returns EOF
123

関連項目

[virtual]
関連付けられた入力シーケンスから文字を読み取り、取得領域に格納する
( std::basic_streambuf<CharT,Traits> の仮想protectedメンバ関数)
[virtual]
入力シーケンスから利用可能な次の文字を返す
( std::basic_stringbuf<CharT,Traits,Allocator> の仮想protectedメンバ関数)
[virtual]
関連付けられたファイルから読み取る
( std::basic_filebuf<CharT,Traits> の仮想protectedメンバ関数)
入力シーケンスから1文字を読み取り、シーケンスを進めない
( std::basic_streambuf<CharT,Traits> のpublicメンバ関数)
文字を抽出する
( std::basic_istream<CharT,Traits> のpublicメンバ関数)