Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_streambuf<CharT,Traits>::overflow

      From cppreference.com
      <cpp‎ |io‎ |basic streambuf
       
       
       
      std::basic_streambuf
       
      protected:
      virtual int_type overflow( int_type ch= Traits::eof());

      The intent of this function is to transmit characters from theput area of the stream buffer to theassociated character sequence.

      Formally, this function ensures that there is space at the put area for at least one character. The base class version always fails, and a possibly-succeeding implementation can only be provided in derived classes (seeimplementation requirements). The standard library providesstd::strstreambuf::overflow(),(until C++26)std::basic_stringbuf::overflow() andstd::basic_filebuf::overflow().

      Contents

      [edit]Parameters

      ch - the character to store in the put area

      [edit]Return value

      Traits::eof()

      [edit]Implementation requirements

      Every overriding definition of this virtual function must obey the following constraints, otherwise the behavior is undefined:

      • The effect of the function is to consume some initial subsequence of the characters of thepending sequence . The pending sequence is defined as the concatenation of the following sequences:
        • The put area (formally, empty sequence ifpbase() is null, otherwisepptr()- pbase() characters beginning atpbase()).
        • The characterch or nothing if ch is EOF (formally, ifTraits::eq_int_type(ch, Traits::eof()) returnstrue).
      • After consumption, the put area pointers are updated to hold the remaining characters, if any. Formally, letr be the number of characters in the pending sequence not consumed:
        • Ifr is non-zero, thenpbase() andpptr() are set so that all following conditions are satisfied:
          • pptr()- pbase() isr.
          • Ther characters starting atpbase() are the associated output stream.
        • Ifr is zero (all characters of the pending sequence have been consumed), then eitherpbase() is set to a null value, orpbase() andpptr() are both set to the same non-null value.
      • The function may fail if either appending some character to the associated output stream fails or if it is unable to establishpbase() andpptr() according to the above rules.
      • If the function succeeds, returns some value other thanTraits::eof(). Typically,ch is returned to indicate success, except whenTraits::eq_int_type(ch, Traits::eof()) returnstrue, in which caseTraits::not_eof(ch) is returned.
      • If the function fails, returnsTraits::eof() or throws an exception.

      [edit]Note

      Thesputc() andsputn() call this function in case of an overflow (pptr()== nullptr orpptr()>= epptr()).

      [edit]Example

      Run this code
      #include <array>#include <cstddef>#include <iostream> // Buffer for std::ostream implemented by std::arraytemplate<std::size_t size,class CharT=char>struct ArrayedStreamBuffer:std::basic_streambuf<CharT>{using Base=std::basic_streambuf<CharT>;using char_type=typename Base::char_type;using int_type=typename Base::int_type;     ArrayedStreamBuffer(){// put area pointers to work with 'buffer'        Base::setp(buffer.data(), buffer.data()+ size);}     int_type overflow(int_type ch){std::cout<<"overflow\n";return Base::overflow(ch);} void print_buffer(){for(char_type i: buffer){if(i==0)std::cout<<"\\0";elsestd::cout<< i;std::cout<<' ';}std::cout<<'\n';} private:std::array<char_type, size> buffer{};// value-initialize buffer}; int main(){    ArrayedStreamBuffer<10> streambuf;std::ostream stream(&streambuf);     stream<<"hello";    streambuf.print_buffer();if(stream.good())std::cout<<"stream is good\n";     stream<<"world";    streambuf.print_buffer();if(stream.good())std::cout<<"stream is good\n";     stream<<"!";    streambuf.print_buffer();if(!stream.good())std::cout<<"stream is not good\n";}

      Output:

      h e l l o \0 \0 \0 \0 \0stream is goodh e l l o w o r l d stream is goodoverflowh e l l o w o r l d stream is not good

      [edit]See also

      [virtual]
      reads characters from the associated input sequence to the get area and advances the next pointer
      (virtual protected member function)[edit]
      [virtual]
      reads characters from the associated input sequence to the get area
      (virtual protected member function)[edit]
      [virtual]
      writes characters to the associated file from the put area
      (virtual protected member function ofstd::basic_filebuf<CharT,Traits>)[edit]
      [virtual]
      appends a character to the output sequence
      (virtual protected member function ofstd::basic_stringbuf<CharT,Traits,Allocator>)[edit]
      [virtual]
      appends a character to the output sequence, may reallocate or initially allocate the buffer if dynamic and not frozen
      (virtual protected member function ofstd::strstreambuf)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_streambuf/overflow&oldid=182218"

      [8]ページ先頭

      ©2009-2025 Movatter.jp