Share via


basic_ostream Class

This template class describes an object that controls insertion of elements and encoded objects into a stream buffer with elements of type Elem, also known as char_type, whose character traits are determined by the class Tr, also known as traits_type.

template <class _Elem, class _Tr = char_traits<Elem> > 
   class basic_ostream 
       : virtual public basic_ios<_Elem, _Tr>

Parameters

  • _Elem
    A char_type.

  • _Tr
    The character traits_type.

Remarks

Most of the member functions that overload operator<< are formatted output functions. They follow the pattern:

   iostate state = goodbit;
   const sentry ok( *this );
   if ( ok )
      {try
         {<convert and insert elements
         accumulate flags in state> }
      catch ( ... )
         {try
           {setstate( badbit ); }
         catch ( ... )
           {}
         if ( ( exceptions( ) & badbit ) != 0 )
           throw; }}
   width( 0 );    // Except for operator<<(Elem)
   setstate( state );
   return ( *this );

Two other member functions are unformatted output functions. They follow the pattern:

   iostate state = goodbit;
   const sentry ok( *this );
   if ( !ok )
      state |= badbit;
   else
      {try
         {<obtain and insert elements
         accumulate flags in state> }
      catch ( ... )
         {try
            {setstate( badbit ); }
         catch ( ... )
            {}
         if ( ( exceptions( ) & badbit ) != 0 )
            throw; }}
   setstate( state );
   return ( *this );

Both groups of functions call setstate(badbit) if they encounter a failure while inserting elements.

An object of class basic_istream<Elem, Tr> stores only a virtual public base object of class basic_ios<Elem, Tr>.

Example

See the example for basic_ofstream Class to learn more about output streams.

Constructors

basic_ostream

Constructs a basic_ostream object.

Member Functions

flush

Flushes the buffer.

put

Puts a character in a stream.

seekp

Resets position in output stream.

sentry

The nested class describes an object whose declaration structures the formatted output functions and the unformatted output functions.

swap

Exchanges the values of this basic_ostream object for those of the provided basic_ostream object.

tellp

Reports position in output stream.

write

Puts characters in a stream.

Operators

operator=

Assigns the value of the provided basic_ostream object parameter to this object.

operator<<

Writes to the stream.

Requirements

Header: <ostream>

Namespace: std

See Also

Reference

Thread Safety in the C++ Standard Library

iostream Programming

iostreams Conventions