class template
<streambuf> <iostream>

std::basic_streambuf

template <class charT, class traits = char_traits<charT> >
  class basic_streambuf;
Base buffer class for streams

This template is designed as base virtual class for all stream buffer classes.

A stream buffer is an object in charge of performing the reading and writing operations of the stream object it is associated with: the stream delegates all such operations to its associated stream buffer object, which is an intermediary between the stream and its controlled input and output sequences.

All stream objects, no matter whether buffered or unbuffered, have an associated stream buffer: Some stream buffer types may then be set to either use an intermediate buffer or not.

Stream buffer objects keep internally, at least:
  • A locale object, used for locale-dependent operations.
  • A set of internal pointers to keep an input buffer: eback, gptr, egptr.
  • A set of internal pointers to keep an output buffer: pbase, pptr, epptr.

Internally, the basic_streambuf class is an elaborated base class designed to provide a uniform public interface for all derived classes: These public functions call virtual protected members that derived classes may override to implement specific behavior. These overridden virtual functions have access to the internals of the basic_streambuf class by means of a set of protected functions (see below).

Template parameters

charT
Character type.
This shall be a non-array POD type.
Aliased as member type basic_streambuf::char_type.
traits
Character traits class that defines essential properties of the characters used by stream objects (see char_traits).
traits::char_type shall be the same as charT.
Aliased as member type basic_streambuf::traits_type.

Template instantiations

These instantiations are declared in <streambuf>, which is included by reference in <iostream>.

Member types

member typedefinitionnotes
char_typeThe first template parameter (charT)
traits_typeThe second template parameter (traits)defaults to: char_traits<charT>
int_typetraits_type::int_type
pos_typetraits_type::pos_typegenerally, the same as streampos
off_typetraits_type::off_typegenerally, the same as streamoff

Public member functions

The common functionality for all stream buffers is provided through the following public member functions:

Locales:
Buffer management and positioning:
Input functions (get):
Output functions (put):

Protected member functions

The public functions do not perform their operations directly on the controlled input and output sequences, but mostly rely on two arrays accessible by a set of internal pointers:

beginning
(beginning pointers)
current position
(get/put pointer)
end
(end pointers)
Input sequenceebackgptregptr
Output sequencepbasepptrepptr

The following protected member functions provide access to these pointers:

Input sequence (get):
Output sequence (put):
Copying:

Virtual protected member functions

Each streambuf-derived class shall define members that keep the validity of the pointers above with respect to their own type of controlled sequence; Modifying the values of the pointers, reallocating the sequences themselves and perfoming all necessary synchronizations with the associated character sequence.

With this design, the core functionality involving the process of reading and writing directly to the specific associated character sequence and to manage the controlled sequences is provided by means of virtual functions, which are overriden as necessary by derived classes:

Locales:

Buffer management and positioning:

Input functions (get):

Output functions (put):