Namespaces
Variants

std::basic_stringbuf<CharT,Traits,Allocator>:: overflow

From cppreference.net
protected :
virtual int_type overflow ( int_type c = Traits :: eof ( ) ) ;

出力文字シーケンスに文字 c を追加します。

c がEOFインジケータ( traits :: eq_int_type ( c, traits :: eof ( ) ) == true )である場合、追加する文字は存在しません。この関数は何も行わず、 traits :: eof ( ) 以外の未規定の値を返します。

それ以外の場合、出力シーケンスに書き込み位置が利用可能であるか、この関数が書き込み位置を利用可能にできる場合、 sputc ( c ) を呼び出し、 c を返します。

この関数は、 std::stringbuf が出力用にオープンされている場合( ( mode & ios_base :: out ) ! = 0 )、書き込み位置を利用可能にします。この場合、現在のバッファ全体と少なくとも1文字以上の追加文字を保持できる十分な大きさのバッファを再割り当て(または初期割り当て)します。 std::stringbuf が入力用にもオープンされている場合( ( mode & ios_base :: in ) ! = 0 )、 overflow egptr() を新しい書き込み位置の直後を指すように移動することで、取得領域のサイズも増加させます。

目次

パラメータ

c - 格納する文字

戻り値

Traits :: eof ( ) は失敗を示すために、 c は文字 c が正常に追加された場合、または Traits :: eof ( ) 以外の何らかの値が、引数として Traits :: eof ( ) が指定された場合に返されます。

注記

この関数は、通常の overflow() とは異なります。通常のoverflow()はバッファの内容を関連付けられた文字シーケンスに移動しますが、 std::basic_stringbuf の場合、バッファと関連付けられたシーケンスは同一であるためです。


この例を実行するために使用された実装(例:GCC-4.9)では、 overflow() はput領域を512バイトにオーバーアロケートします: str() の呼び出しは初期化された4バイトのみを返しますが、次の508回の sputc() 呼び出しでは新たな overflow() の呼び出しは必要ありません。

#include <sstream>
#include <iostream>
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 = EOF) override
    {
        std::cout << "stringbuf::overflow('" << char(c) << "') called\n"
                  << "Before: size of get area: " << egptr() - eback() << '\n'
                  << "        size of put area: " << epptr() - pbase() << '\n';
        int_type ret = std::stringbuf::overflow(c);
        std::cout << "After : size of get area: " << egptr() - eback() << '\n'
                  << "        size of put area: " << epptr() - pbase() << '\n';
        return ret;
    }
};
int main()
{
    std::cout << "read-write stream:\n";
    mybuf sbuf("   "); // read-write stream
    std::iostream stream(&sbuf);
    stream << 1234;
    std::cout << sbuf.str() << '\n';
    std::cout << "\nread-only stream:\n";
    mybuf ro_buf("   ", std::ios_base::in); // read-only stream
    std::iostream ro_stream(&ro_buf);
    ro_stream << 1234;
    std::cout << "\nwrite-only stream:\n";
    mybuf wr_buf("   ", std::ios_base::out); // write-only stream
    std::iostream wr_stream(&wr_buf);
    wr_stream << 1234;
}

出力例:

read-write stream:
stringbuf::overflow('4') called
Before: size of get area: 3
        size of put area: 3
After : size of get area: 4
        size of put area: 512
1234
read-only stream:
stringbuf::overflow('1') called
Before: size of get area: 3
        size of put area: 0
After : size of get area: 3
        size of put area: 0
write-only stream:
stringbuf::overflow('4') called
Before: size of get area: 0
        size of put area: 3
After : size of get area: 0
        size of put area: 512

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 169 C++98 バッファの(再)割り当てで追加できる文字は1文字のみ より多くの追加文字を許可
LWG 432 C++98 overflow epptr() を新しい書き込み位置の直後に移動
(入力用に開かれた std::stringbuf の場合)
移動しない

関連項目

[virtual]
パット領域から関連付けられた出力シーケンスへ文字を書き込む
( std::basic_streambuf<CharT,Traits> の仮想保護メンバ関数)
[virtual]
入力シーケンスで利用可能な次の文字を返す
(仮想保護メンバ関数)