From cppreference.com
protected:virtualint_typeoverflow(int_typech=Traits::eof());The intent of this function is to transmit characters from the
of the stream buffer to the
.
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 (see
). The standard library provides
, (until C++26)
std::basic_stringbuf::overflow()
and
std::basic_filebuf::overflow()
.
Parameters
ch - the character to store in the put area Return value
Traits::eof()
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 the pending sequence . The pending sequence is defined as the concatenation of the following sequences: The put area (formally, empty sequence if
is null, otherwise pptr()-pbase() characters beginning at
).
The character ch or nothing if ch is EOF (formally, if Traits::eq_int_type(ch,Traits::eof()) returns true).
After consumption, the put area pointers are updated to hold the remaining characters, if any. Formally, let r be the number of characters in the pending sequence not consumed: If r is non-zero, then
and
are set so that all following conditions are satisfied: pptr()-pbase() is r.
The r characters starting at
are the associated output stream.
If r is zero (all characters of the pending sequence have been consumed), then either
is set to a null value, or
and
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 establish
and
according to the above rules.
If the function succeeds, returns some value other than Traits::eof(). Typically, ch is returned to indicate success, except when Traits::eq_int_type(ch,Traits::eof()) returns true, in which case Traits::not_eof(ch) is returned.
If the function fails, returns Traits::eof() or throws an exception.
Note
The
and
call this function in case of an overflow (pptr()==nullptr or pptr()>=epptr()).
Example
Run this code
#include<array>#include<cstddef>#include<iostream>// Buffer for std::ostream implemented by std::arraytemplate<std::size_tsize,classCharT=char>structArrayedStreamBuffer:std::basic_streambuf<CharT>{usingBase=std::basic_streambuf<CharT>;usingchar_type=typenameBase::char_type;usingint_type=typenameBase::int_type;ArrayedStreamBuffer(){// put area pointers to work with 'buffer'Base::setp(buffer.data(),buffer.data()+size);}int_typeoverflow(int_typech){std::cout<<"overflow\n";returnBase::overflow(ch);}voidprint_buffer(){for(char_typei: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};intmain(){ArrayedStreamBuffer<10>streambuf;std::ostreamstream(&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 \0 stream is good h e l l o w o r l d stream is good overflow h e l l o w o r l d stream is not good See also
[virtual]
reads characters from the associated input sequence to the get area and advances the next pointer
(virtual protected member function)
[virtual]
reads characters from the associated input sequence to the get area
(virtual protected member function)
[virtual]
writes characters to the associated file from the put area
(virtual protected member function of std::basic_filebuf<CharT,Traits>)
[virtual]
appends a character to the output sequence
(virtual protected member function of std::basic_stringbuf<CharT,Traits,Allocator>)
[virtual]
appends a character to the output sequence, may reallocate or initially allocate the buffer if dynamic and not frozen
(virtual protected member function of std::strstreambuf)