29 Input/output library [input.output]

29.7 Formatting and manipulators [iostream.format]

29.7.5 Output streams [output.streams]

29.7.5.1 Class template basic_­ostream [ostream]

namespace std {
  template<class charT, class traits = char_traits<charT>>
  class basic_ostream : virtual public basic_ios<charT, traits> {
  public:
    // types (inherited from basic_­ios)
    using char_type   = charT;
    using int_type    = typename traits::int_type;
    using pos_type    = typename traits::pos_type;
    using off_type    = typename traits::off_type;
    using traits_type = traits;

    // [ostream.cons], constructor/destructor
    explicit basic_ostream(basic_streambuf<char_type, traits>* sb);
    virtual ~basic_ostream();

    // [ostream.sentry], prefix/suffix
    class sentry;

    // [ostream.formatted], formatted output
    basic_ostream<charT, traits>&
      operator<<(basic_ostream<charT, traits>& (*pf)(basic_ostream<charT, traits>&));
    basic_ostream<charT, traits>&
      operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
    basic_ostream<charT, traits>&
      operator<<(ios_base& (*pf)(ios_base&));

    basic_ostream<charT, traits>& operator<<(bool n);
    basic_ostream<charT, traits>& operator<<(short n);
    basic_ostream<charT, traits>& operator<<(unsigned short n);
    basic_ostream<charT, traits>& operator<<(int n);
    basic_ostream<charT, traits>& operator<<(unsigned int n);
    basic_ostream<charT, traits>& operator<<(long n);
    basic_ostream<charT, traits>& operator<<(unsigned long n);
    basic_ostream<charT, traits>& operator<<(long long n);
    basic_ostream<charT, traits>& operator<<(unsigned long long n);
    basic_ostream<charT, traits>& operator<<(float f);
    basic_ostream<charT, traits>& operator<<(double f);
    basic_ostream<charT, traits>& operator<<(long double f);

    basic_ostream<charT, traits>& operator<<(const void* p);
    basic_ostream<charT, traits>& operator<<(nullptr_t);
    basic_ostream<charT, traits>& operator<<(basic_streambuf<char_type, traits>* sb);

    // [ostream.unformatted], unformatted output
    basic_ostream<charT, traits>& put(char_type c);
    basic_ostream<charT, traits>& write(const char_type* s, streamsize n);

    basic_ostream<charT, traits>& flush();

    // [ostream.seeks], seeks
    pos_type tellp();
    basic_ostream<charT, traits>& seekp(pos_type);
    basic_ostream<charT, traits>& seekp(off_type, ios_base::seekdir);

  protected:
    // [ostream.cons], copy/move constructor
    basic_ostream(const basic_ostream&) = delete;
    basic_ostream(basic_ostream&& rhs);

    // [ostream.assign], assign and swap
    basic_ostream& operator=(const basic_ostream&) = delete;
    basic_ostream& operator=(basic_ostream&& rhs);
    void swap(basic_ostream& rhs);
  };

  // [ostream.inserters.character], character inserters
  template<class charT, class traits>
    basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&, charT);
  template<class charT, class traits>
    basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&, char);
  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char);

  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, signed char);
  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, unsigned char);

  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, wchar_t) = delete;
  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char8_t) = delete;
  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char16_t) = delete;
  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char32_t) = delete;
  template<class traits>
    basic_ostream<wchar_t, traits>&
      operator<<(basic_ostream<wchar_t, traits>&, char8_t) = delete;
  template<class traits>
    basic_ostream<wchar_t, traits>&
      operator<<(basic_ostream<wchar_t, traits>&, char16_t) = delete;
  template<class traits>
    basic_ostream<wchar_t, traits>&
      operator<<(basic_ostream<wchar_t, traits>&, char32_t) = delete;

  template<class charT, class traits>
    basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&, const charT*);
  template<class charT, class traits>
    basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&, const char*);
  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char*);

  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const signed char*);
  template<class traits>
    basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const unsigned char*);

  template<class traits>
    basic_ostream<char, traits>&
      operator<<(basic_ostream<char, traits>&, const wchar_t*) = delete;
  template<class traits>
    basic_ostream<char, traits>&
      operator<<(basic_ostream<char, traits>&, const char8_t*) = delete;
  template<class traits>
    basic_ostream<char, traits>&
      operator<<(basic_ostream<char, traits>&, const char16_t*) = delete;
  template<class traits>
    basic_ostream<char, traits>&
      operator<<(basic_ostream<char, traits>&, const char32_t*) = delete;
  template<class traits>
    basic_ostream<wchar_t, traits>&
      operator<<(basic_ostream<wchar_t, traits>&, const char8_t*) = delete;
  template<class traits>
    basic_ostream<wchar_t, traits>&
      operator<<(basic_ostream<wchar_t, traits>&, const char16_t*) = delete;
  template<class traits>
    basic_ostream<wchar_t, traits>&
      operator<<(basic_ostream<wchar_t, traits>&, const char32_t*) = delete;
}
The class template basic_­ostream defines a number of member function signatures that assist in formatting and writing output to output sequences controlled by a stream buffer.
Two groups of member function signatures share common properties: the formatted output functions (or inserters) and the unformatted output functions. Both groups of output functions generate (or insert) output characters by actions equivalent to calling rdbuf()->sputc(int_­type).
They may use other public members of basic_­ostream except that they shall not invoke any virtual members of rdbuf() except overflow(), xsputn(), and sync().
If one of these called functions throws an exception, then unless explicitly noted otherwise the output function sets badbit in the error state.
If badbit is set in exceptions(), the output function rethrows the exception without completing its actions, otherwise it does not throw anything and proceeds as if the called function had returned a failure indication.
[Note
:
The deleted overloads of operator<< prevent formatting characters as integers and strings as pointers.
— end note
]

29.7.5.1.1 Constructors [ostream.cons]

explicit basic_ostream(basic_streambuf<charT, traits>* sb);
Effects: Initializes the base class subobject with basic_­ios<charT, traits>​::​init(sb) ([basic.ios.cons]).
Postconditions: rdbuf() == sb.
basic_ostream(basic_ostream&& rhs);
Effects: Move constructs from the rvalue rhs.
This is accomplished by default constructing the base class and calling basic_­ios<charT, traits>​::​move(rhs) to initialize the base class.
virtual ~basic_ostream();
Remarks: Does not perform any operations on rdbuf().

29.7.5.1.2 Assignment and swap [ostream.assign]

basic_ostream& operator=(basic_ostream&& rhs);
Effects: Equivalent to: swap(rhs).
Returns: *this.
void swap(basic_ostream& rhs);
Effects: Calls basic_­ios<charT, traits>​::​swap(rhs).

29.7.5.1.3 Class basic_­ostream​::​sentry [ostream.sentry]

namespace std {
  template<class charT, class traits = char_traits<charT>>
  class basic_ostream<charT, traits>::sentry {
    bool ok_;       // exposition only
  public:
    explicit sentry(basic_ostream<charT, traits>& os);
    ~sentry();
    explicit operator bool() const { return ok_; }

    sentry(const sentry&) = delete;
    sentry& operator=(const sentry&) = delete;
  };
}
The class sentry defines a class that is responsible for doing exception safe prefix and suffix operations.
explicit sentry(basic_ostream<charT, traits>& os);
If os.good() is nonzero, prepares for formatted or unformatted output.
If os.tie() is not a null pointer, calls os.tie()->flush().314
If, after any preparation is completed, os.good() is true, ok_­ == true otherwise, ok_­ == false.
During preparation, the constructor may call setstate(failbit) (which may throw ios_­base​::​​failure ([iostate.flags])).315
~sentry();
If (os.flags() & ios_­base​::​unitbuf) && !uncaught_­exceptions() && os.good() is true, calls os.rdbuf()->pubsync().
If that function returns -1, sets badbit in os.rdstate() without propagating an exception.
explicit operator bool() const;
Effects: Returns ok_­.
The call os.tie()->flush() does not necessarily occur if the function can determine that no synchronization is necessary.
The sentry constructor and destructor can also perform additional implementation-dependent operations.

29.7.5.1.4 Seek members [ostream.seeks]

Each seek member function begins execution by constructing an object of class sentry.
It returns by destroying the sentry object.
pos_type tellp();
Returns: If fail() != false, returns pos_­type(-1) to indicate failure.
Otherwise, returns rdbuf()->​pubseekoff(0, cur, out).
basic_ostream<charT, traits>& seekp(pos_type pos);
Effects: If fail() != true, executes rdbuf()->pubseekpos(pos, ios_­base​::​out).
In case of failure, the function calls setstate(failbit) (which may throw ios_­base​::​failure).
Returns: *this.
basic_ostream<charT, traits>& seekp(off_type off, ios_base::seekdir dir);
Effects: If fail() != true, executes rdbuf()->pubseekoff(off, dir, ios_­base​::​out).
In case of failure, the function calls setstate(failbit) (which may throw ios_­base​::​failure).
Returns: *this.