[ostream] (original) (raw)

31 Input/output library [input.output]

31.7 Formatting and manipulators [iostream.format]

31.7.6 Output streams [output.streams]

31.7.6.2 Class template basic_ostream [ostream]


31.7.6.2.1 General [ostream.general]

31.7.6.2.2 Constructors [ostream.cons]

31.7.6.2.3 Assignment and swap [ostream.assign]

31.7.6.2.4 Class basic_ostream​::​sentry [ostream.sentry]

31.7.6.2.5 Seek members [ostream.seeks]


31.7.6.2.1 General [ostream.general]

When a function has a parameter type extended-floating-point-type, the implementation provides overloads for all cv-unqualified extended floating-point types ([basic.fundamental]).

namespace std { template<class charT, class traits = char_traits<charT>> class basic_ostream : virtual public basic_ios<charT, traits> { public: using char_type = charT;using int_type = traits::int_type;using pos_type = traits::pos_type;using off_type = traits::off_type;using traits_type = traits;explicit basic_ostream(basic_streambuf<char_type, traits>* sb);virtual ~basic_ostream();class sentry; basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&)); basic_ostream& operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&)); basic_ostream& operator<<(ios_base& (*pf)(ios_base&)); basic_ostream& operator<<(bool n); basic_ostream& operator<<(short n); basic_ostream& operator<<(unsigned short n); basic_ostream& operator<<(int n); basic_ostream& operator<<(unsigned int n); basic_ostream& operator<<(long n); basic_ostream& operator<<(unsigned long n); basic_ostream& operator<<(long long n); basic_ostream& operator<<(unsigned long long n); basic_ostream& operator<<(float f); basic_ostream& operator<<(double f); basic_ostream& operator<<(long double f); basic_ostream& operator<<(_extended-floating-point-type_ f); basic_ostream& operator<<(const void* p); basic_ostream& operator<<(const volatile void* p); basic_ostream& operator<<(nullptr_t); basic_ostream& operator<<(basic_streambuf<char_type, traits>* sb); basic_ostream& put(char_type c); basic_ostream& write(const char_type* s, streamsize n); basic_ostream& flush(); pos_type tellp(); basic_ostream& seekp(pos_type); basic_ostream& seekp(off_type, ios_base::seekdir);protected: basic_ostream(const basic_ostream&) = delete; basic_ostream(basic_ostream&& rhs); basic_ostream& operator=(const basic_ostream&) = delete; basic_ostream& operator=(basic_ostream&& rhs);void swap(basic_ostream& rhs);};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 templatebasic_ostreamdefines 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: theformatted output functions(orinserters) and theunformatted output functions.Both groups of output functions generate (orinsert) outputcharactersby actions equivalent to callingrdbuf()->sputc(int_type).

They may use other public members ofbasic_ostreamexcept that they shall not invoke any virtual members ofrdbuf()exceptoverflow(),xsputn(), andsync().

If one of these called functions throws an exception, then unless explicitly noted otherwise the output function setsbadbitin the error state.

Ifbadbitis set inexceptions(), 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 1:

The deleted overloads of operator<<prevent formatting characters as integers and strings as pointers.

— _end note_]

31.7.6.2.2 Constructors [ostream.cons]

explicit basic_ostream(basic_streambuf<charT, traits>* sb);

Effects: Initializes the base class subobject withbasic_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 callingbasic_ios<charT, traits>​::​move(rhs) to initialize the base class.

virtual ~basic_ostream();

Remarks: Does not perform any operations onrdbuf().

31.7.6.2.3 Assignment and swap [ostream.assign]

basic_ostream& operator=(basic_ostream&& rhs);

Effects: Equivalent to swap(rhs).

void swap(basic_ostream& rhs);

Effects: Calls basic_ios<charT, traits>​::​swap(rhs).

31.7.6.2.4 Class basic_ostream​::​sentry [ostream.sentry]

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

The classsentrydefines a class that is responsible for doing exception safe prefix and suffix operations.

explicit sentry(basic_ostream& os);

Ifos.good()is nonzero, prepares for formatted or unformatted output.

Ifos.tie()is not a null pointer, callsos.tie()->flush().283

If, after any preparation is completed,os.good()istrue,ok_ == trueotherwise,ok_ == false.

During preparation, the constructor may callsetstate(failbit)(which may throwios_base​::​​failure ([iostate.flags])).284

If(os.flags() & ios_base​::​unitbuf) && !uncaught_exceptions() && os.good()istrue, callsos.rdbuf()->pubsync().

If that function returns or exits via an exception, sets badbit inos.rdstate() without propagating an exception.

explicit operator bool() const;

31.7.6.2.5 Seek members [ostream.seeks]

Each seek member function begins execution by constructing an object of class sentry.

It returns by destroying the sentry object.

Returns: Iffail() != false, returnspos_type(-1)to indicate failure.

Otherwise, returnsrdbuf()->​pubseekoff(​0, cur, out).

basic_ostream& seekp(pos_type pos);

Effects: Iffail() != true, executesrdbuf()->pubseekpos(pos, ios_base​::​out).

In case of failure, the function callssetstate(failbit)(which may throwios_base​::​failure).

basic_ostream& seekp(off_type off, ios_base::seekdir dir);

Effects: Iffail() != true, executesrdbuf()->pubseekoff(off, dir, ios_base​::​out).

In case of failure, the function calls setstate(failbit)(which may throw ios_base​::​failure).