• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <streambuf>

    std::basic_streambuf::overflow

    namespace std {
      template<class CharT, class Traits = char_traits<CharT>>
      class basic_streambuf {
      protected:
        virtual int_type overflow(int_type c = Traits::eof());
    
        ……
      };
    }
    

    概要

    出力部分列の領域を消費し切った際の処理。

    効果

    デフォルトの動作はTraits::eof()を返すのみ。(この関数をオーバーライドすることで、関数内で改めてsetp()を呼んで別の出力部分列を指し直す、のような処理を追加できる。)

    戻り値

    Traits::eof()

    備考

    出力部分列の領域を消費し切った状態でsputc()を呼ぶと、overflow()が呼ばれる。sputn()がoverflow()を呼ぶかどうかは未規定

    #include <iostream>
    #include <streambuf>
    
    // streambufを継承したクラス
    class dummy_buf : public std::streambuf {
    public:
        dummy_buf(char* begin, char* end) {
            // 出力列の領域を指定
            setp(begin, end);
        }
    protected:
        int_type overflow(int_type c) override {
            std::cout << "overflow" << std::endl;
            // ベースクラスのoverflow()を呼ぶ
            return std::streambuf::overflow(c);
        }
    };
    
    int main(void) {
        char space[10] = {};
        dummy_buf buf{space, space + 2};
        std::cout << buf.sputc('A') << std::endl;  // 'A'を書き込む
        std::cout << buf.sputc('B') << std::endl;  // 'B'を書き込む
        std::cout << buf.sputc('C') << std::endl;  // 'C'の書き込みに失敗。overflow()が呼ばれ、Traits::eof()が返る
    }
    

    出力

    65
    66
    overflow
    -1
    

    バージョン

    言語

    • C++98

    参照