protected:
virtual int_type overflow( int_type ch = Traits::eof() );
The intent of this function is to transmit characters from the put area of the stream buffer to the associated 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 (see implementation requirements). The standard library provides std::strstreambuf::overflow(), (until C++26)std::basic_stringbuf::overflow() and std::basic_filebuf::overflow().
[edit] Parameters ch - the character to store in the put area [edit] Return valueTraits::eof()
[edit] Implementation requirementsEvery overriding definition of this virtual function must obey the following constraints, otherwise the behavior is undefined:
The sputc() and sputn() call this function in case of an overflow (pptr() == nullptr or pptr() >= epptr()).
[edit] Example#include <array> #include <cstddef> #include <iostream> // Buffer for std::ostream implemented by std::array template<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"; else std::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 \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[edit] See also reads characters from the associated input sequence to the get area and advances the next pointer
std::basic_filebuf<CharT,Traits>
) [edit] appends a character to the output sequence
std::basic_stringbuf<CharT,Traits,Allocator>
) [edit] appends a character to the output sequence, may reallocate or initially allocate the buffer if dynamic and not frozen
std::strstreambuf
) [edit]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4