[ostream.formatted] # 31 Input/output library [[input.output]](./#input.output) ## 31.7 Formatting and manipulators [[iostream.format]](iostream.format#ostream.formatted) ### 31.7.6 Output streams [[output.streams]](output.streams#ostream.formatted) #### 31.7.6.3 Formatted output functions [ostream.formatted] #### [31.7.6.3.1](#reqmts) Common requirements [[ostream.formatted.reqmts]](ostream.formatted.reqmts) [1](#reqmts-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6354) Each formatted output function begins execution by constructing an object of classsentry[.](#reqmts-1.sentence-1) If that object returnstrue when converted to a value of typebool, the function endeavors to generate the requested output[.](#reqmts-1.sentence-2) If the generation fails, then the formatted output function doessetstate(ios_base​::​failbit), which can throw an exception[.](#reqmts-1.sentence-3) If an exception is thrown during output, thenios_base​::​badbit is set[285](#footnote-285 "This is done without causing an ios_­base​::​failure to be thrown.") in*this's error state[.](#reqmts-1.sentence-4) If(exceptions() & badbit) != 0 then the exception is rethrown[.](#reqmts-1.sentence-5) Whether or not an exception is thrown, thesentry object is destroyed before leaving the formatted output function[.](#reqmts-1.sentence-6) If no exception is thrown, the result of the formatted output function is*this[.](#reqmts-1.sentence-7) [2](#reqmts-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6387) The descriptions of the individual formatted output functions describe how they perform output and do not mention thesentry object[.](#reqmts-2.sentence-1) [3](#reqmts-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6394) If a formatted output function of a stream os determines padding, it does so as follows[.](#reqmts-3.sentence-1) Given a charT character sequence seq wherecharT is the character container type of the stream, if the length of seq is less than os.width(), then enough copies ofos.fill() are added to this sequence as necessary to pad to a width of os.width() characters[.](#reqmts-3.sentence-2) If(os.flags() & ios_base​::​adjustfield) == ios_base​::​left istrue, the fill characters are placed after the character sequence; otherwise, they are placed before the character sequence[.](#reqmts-3.sentence-3) [285)](#footnote-285)[285)](#footnoteref-285) This is done without causing anios_base​::​failure to be thrown[.](#footnote-285.sentence-1) #### [31.7.6.3.2](#ostream.inserters.arithmetic) Arithmetic inserters [[ostream.inserters.arithmetic]](ostream.inserters.arithmetic) [🔗](#lib:operator%3c%3c,basic_ostream) `basic_ostream& operator<<(bool val); basic_ostream& operator<<(short val); basic_ostream& operator<<(unsigned short val); basic_ostream& operator<<(int val); basic_ostream& operator<<(unsigned int val); basic_ostream& operator<<(long val); basic_ostream& operator<<(unsigned long val); basic_ostream& operator<<(long long val); basic_ostream& operator<<(unsigned long long val); basic_ostream& operator<<(float val); basic_ostream& operator<<(double val); basic_ostream& operator<<(long double val); basic_ostream& operator<<(const void* val); ` [1](#ostream.inserters.arithmetic-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6427) *Effects*: The classesnum_get<> andnum_put<> handle locale-dependent numeric formatting and parsing[.](#ostream.inserters.arithmetic-1.sentence-1) These inserter functions use the imbuedlocale value to perform numeric formatting[.](#ostream.inserters.arithmetic-1.sentence-2) When val is of typebool,long,unsigned long,long long, unsigned long long,double,long double, orconst void*, the formatting conversion occurs as if it performed the following code fragment:bool failed = use_facet>>( getloc()).put(*this, *this, fill(), val).failed(); When val is of typeshort the formatting conversion occurs as if it performed the following code fragment:ios_base::fmtflags baseflags = ios_base::flags() & ios_base::basefield;bool failed = use_facet>>( getloc()).put(*this, *this, fill(), baseflags == ios_base::oct || baseflags == ios_base::hex ? static_cast(static_cast(val)): static_cast(val)).failed(); When val is of typeint the formatting conversion occurs as if it performed the following code fragment:ios_base::fmtflags baseflags = ios_base::flags() & ios_base::basefield;bool failed = use_facet>>( getloc()).put(*this, *this, fill(), baseflags == ios_base::oct || baseflags == ios_base::hex ? static_cast(static_cast(val)): static_cast(val)).failed(); When val is of typeunsigned short orunsigned int the formatting conversion occurs as if it performed the following code fragment:bool failed = use_facet>>( getloc()).put(*this, *this, fill(), static_cast(val)).failed(); When val is of typefloat the formatting conversion occurs as if it performed the following code fragment:bool failed = use_facet>>( getloc()).put(*this, *this, fill(), static_cast(val)).failed(); [2](#ostream.inserters.arithmetic-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6496) The first argument provides an object of theostreambuf_iterator<> class which is an iterator for class basic_ostream<>[.](#ostream.inserters.arithmetic-2.sentence-1) It bypassesostreams and usesstreambufs directly[.](#ostream.inserters.arithmetic-2.sentence-2) Classlocale relies on these types as its interface to iostreams, since for flexibility it has been abstracted away from direct dependence onostream[.](#ostream.inserters.arithmetic-2.sentence-3) The second parameter is a reference to the base class subobject of typeios_base[.](#ostream.inserters.arithmetic-2.sentence-4) It provides formatting specifications such as field width, and a locale from which to obtain other facets[.](#ostream.inserters.arithmetic-2.sentence-5) Iffailed istrue then doessetstate(badbit), which may throw an exception, and returns[.](#ostream.inserters.arithmetic-2.sentence-6) [3](#ostream.inserters.arithmetic-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6523) *Returns*: *this[.](#ostream.inserters.arithmetic-3.sentence-1) [🔗](#lib:operator%3c%3c,basic_ostream_) `basic_ostream& operator<<(const volatile void* p); ` [4](#ostream.inserters.arithmetic-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6534) *Effects*: Equivalent to: return operator<<(const_cast(p)); [🔗](#ostream.inserters.arithmetic-itemdecl:3) `basic_ostream& operator<<(extended-floating-point-type val); ` [5](#ostream.inserters.arithmetic-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6544) *Effects*: If the floating-point conversion rank of *extended-floating-point-type* is less than or equal to that of double, the formatting conversion occurs as if it performed the following code fragment:bool failed = use_facet>>( getloc()).put(*this, *this, fill(), static_cast(val)).failed(); Otherwise, if the floating-point conversion rank of *extended-floating-point-type* is less than or equal to that of long double, the formatting conversion occurs as if it performed the following code fragment:bool failed = use_facet>>( getloc()).put(*this, *this, fill(), static_cast(val)).failed(); Otherwise, an invocation of the operator function is conditionally supported with implementation-defined semantics[.](#ostream.inserters.arithmetic-5.sentence-3) If failed is true then does setstate(badbit), which may throw an exception, and returns[.](#ostream.inserters.arithmetic-5.sentence-4) [6](#ostream.inserters.arithmetic-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6568) *Returns*: *this[.](#ostream.inserters.arithmetic-6.sentence-1) #### [31.7.6.3.3](#ostream.inserters) basic_ostream​::​operator<< [[ostream.inserters]](ostream.inserters) [🔗](#lib:operator%3c%3c,basic_ostream__) `basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&)); ` [1](#ostream.inserters-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6581) *Effects*: None[.](#ostream.inserters-1.sentence-1) Does not behave as a formatted output function (as described in [[ostream.formatted.reqmts]](#reqmts "31.7.6.3.1 Common requirements"))[.](#ostream.inserters-1.sentence-2) [2](#ostream.inserters-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6587) *Returns*: pf(*this)[.](#ostream.inserters-2.sentence-1)[286](#footnote-286 "See, for example, the function signature endl(basic_­ostream&) ([ostream.manip]).") [🔗](#lib:operator%3c%3c,basic_ostream___) `basic_ostream& operator<<(basic_ios& (*pf)(basic_ios&)); ` [3](#ostream.inserters-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6603) *Effects*: Callspf(*this)[.](#ostream.inserters-3.sentence-1) This inserter does not behave as a formatted output function (as described in [[ostream.formatted.reqmts]](#reqmts "31.7.6.3.1 Common requirements"))[.](#ostream.inserters-3.sentence-2) [4](#ostream.inserters-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6610) *Returns*: *this[.](#ostream.inserters-4.sentence-1)[287](#footnote-287 "See, for example, the function signature dec(ios_­base&) ([basefield.manip]).") [🔗](#lib:operator%3c%3c,basic_ostream____) `basic_ostream& operator<<(ios_base& (*pf)(ios_base&)); ` [5](#ostream.inserters-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6626) *Effects*: Callspf(*this)[.](#ostream.inserters-5.sentence-1) This inserter does not behave as a formatted output function (as described in [[ostream.formatted.reqmts]](#reqmts "31.7.6.3.1 Common requirements"))[.](#ostream.inserters-5.sentence-2) [6](#ostream.inserters-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6633) *Returns*: *this[.](#ostream.inserters-6.sentence-1) [🔗](#lib:operator%3c%3c,basic_ostream_____) `basic_ostream& operator<<(basic_streambuf* sb); ` [7](#ostream.inserters-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6644) *Effects*: Behaves as an unformatted output function ([[ostream.unformatted]](ostream.unformatted "31.7.6.4 Unformatted output functions"))[.](#ostream.inserters-7.sentence-1) After the sentry object is constructed, ifsb is null callssetstate(badbit) (which may throwios_base​::​failure)[.](#ostream.inserters-7.sentence-2) [8](#ostream.inserters-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6654) Gets characters from sb and inserts them in*this[.](#ostream.inserters-8.sentence-1) Characters are read from sb and inserted until any of the following occurs: - [(8.1)](#ostream.inserters-8.1) end-of-file occurs on the input sequence; - [(8.2)](#ostream.inserters-8.2) inserting in the output sequence fails (in which case the character to be inserted is not extracted); - [(8.3)](#ostream.inserters-8.3) an exception occurs while getting a character from sb[.](#ostream.inserters-8.sentence-2) [9](#ostream.inserters-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6670) If the function inserts no characters, it callssetstate(failbit) (which may throwios_base​::​​failure ([[iostate.flags]](iostate.flags "31.5.4.4 Flags functions")))[.](#ostream.inserters-9.sentence-1) If an exception was thrown while extracting a character, the function setsfailbit in the error state, and iffailbit is set inexceptions() the caught exception is rethrown[.](#ostream.inserters-9.sentence-2) [10](#ostream.inserters-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6684) *Returns*: *this[.](#ostream.inserters-10.sentence-1) [🔗](#lib:operator%3c%3c,basic_ostream______) `basic_ostream& operator<<(nullptr_t); ` [11](#ostream.inserters-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6695) *Effects*: Equivalent to:return *this << s; where s is animplementation-defined[NTCTS](defns.ntcts "3.36 NTCTS [defns.ntcts]")[.](#ostream.inserters-11.sentence-1) [286)](#footnote-286)[286)](#footnoteref-286) See, for example, the function signatureendl(basic_ostream&) ([[ostream.manip]](ostream.manip "31.7.6.5 Standard basic_­ostream manipulators"))[.](#footnote-286.sentence-1) [287)](#footnote-287)[287)](#footnoteref-287) See, for example, the function signaturedec(ios_base&) ([[basefield.manip]](basefield.manip "31.5.5.3 basefield manipulators"))[.](#footnote-287.sentence-1) #### [31.7.6.3.4](#ostream.inserters.character) Character inserter function templates [[ostream.inserters.character]](ostream.inserters.character) [🔗](#lib:operator%3c%3c,basic_ostream_______) `template basic_ostream& operator<<(basic_ostream& out, charT c); template basic_ostream& operator<<(basic_ostream& out, char c); // specialization template basic_ostream& operator<<(basic_ostream& out, char c); // signed and unsigned template basic_ostream& operator<<(basic_ostream& out, signed char c); template basic_ostream& operator<<(basic_ostream& out, unsigned char c); ` [1](#ostream.inserters.character-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6725) *Effects*: Behaves as a [formatted output function](#reqmts "31.7.6.3.1 Common requirements [ostream.formatted.reqmts]") of out[.](#ostream.inserters.character-1.sentence-1) Constructs a character sequence seq[.](#ostream.inserters.character-1.sentence-2) If c has typechar and the character container type of the stream is notchar, then seq consists ofout.widen(c); otherwise seq consists ofc[.](#ostream.inserters.character-1.sentence-3) Determines padding for seq as described in [[ostream.formatted.reqmts]](#reqmts "31.7.6.3.1 Common requirements")[.](#ostream.inserters.character-1.sentence-4) Inserts seq intoout[.](#ostream.inserters.character-1.sentence-5) Calls os.width(0)[.](#ostream.inserters.character-1.sentence-6) [2](#ostream.inserters.character-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6740) *Returns*: out[.](#ostream.inserters.character-2.sentence-1) [🔗](#lib:operator%3c%3c,basic_ostream________) `template basic_ostream& operator<<(basic_ostream& out, const charT* s); template basic_ostream& operator<<(basic_ostream& out, const char* s); template basic_ostream& operator<<(basic_ostream& out, const char* s); template basic_ostream& operator<<(basic_ostream& out, const signed char* s); template basic_ostream& operator<<(basic_ostream& out, const unsigned char* s); ` [3](#ostream.inserters.character-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6761) *Preconditions*: s is not a null pointer[.](#ostream.inserters.character-3.sentence-1) [4](#ostream.inserters.character-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6765) *Effects*: Behaves like a formatted inserter (as described in [[ostream.formatted.reqmts]](#reqmts "31.7.6.3.1 Common requirements")) of out[.](#ostream.inserters.character-4.sentence-1) Creates a character sequence seq of n characters starting at s, each widened usingout.widen() ([[basic.ios.members]](basic.ios.members "31.5.4.3 Member functions")), where n is the number that would be computed as if by: - [(4.1)](#ostream.inserters.character-4.1) traits​::​length(s) for the overload where the first argument is of typebasic_ostream& and the second is of typeconst charT*, and also for the overload where the first argument is of typebasic_ostream& and the second is of typeconst char*, - [(4.2)](#ostream.inserters.character-4.2) char_traits​::​length(s) for the overload where the first argument is of typebasic_ostream& and the second is of typeconst char*, - [(4.3)](#ostream.inserters.character-4.3) traits​::​length(reinterpret_cast(s)) for the other two overloads[.](#ostream.inserters.character-4.sentence-2) Determines padding for seq as described in [[ostream.formatted.reqmts]](#reqmts "31.7.6.3.1 Common requirements")[.](#ostream.inserters.character-4.sentence-3) Inserts seq intoout[.](#ostream.inserters.character-4.sentence-4) Calls width(0)[.](#ostream.inserters.character-4.sentence-5) [5](#ostream.inserters.character-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6797) *Returns*: out[.](#ostream.inserters.character-5.sentence-1) #### [31.7.6.3.5](#print) Print [[ostream.formatted.print]](ostream.formatted.print) [🔗](#lib:print) `template void print(ostream& os, format_string fmt, Args&&... args); ` [1](#print-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6811) *Effects*: If the ordinary literal encoding ([[lex.charset]](lex.charset "5.3.1 Character sets")) is UTF-8, equivalent to:vprint_unicode(os, fmt.*str*, make_format_args(args...)); Otherwise, equivalent to:vprint_nonunicode(os, fmt.*str*, make_format_args(args...)); [🔗](#lib:println) `template void println(ostream& os, format_string fmt, Args&&... args); ` [2](#print-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6830) *Effects*: Equivalent to:print(os, "{}\n", format(os.getloc(), fmt, std::forward(args)...)); [🔗](#lib:println_) `void println(ostream& os); ` [3](#print-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6844) *Effects*: Equivalent to:print(os, "\n"); [🔗](#lib:vprint_unicode) `void vprint_unicode(ostream& os, string_view fmt, format_args args); void vprint_nonunicode(ostream& os, string_view fmt, format_args args); ` [4](#print-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6860) *Effects*: Behaves as a formatted output function ([[ostream.formatted.reqmts]](#reqmts "31.7.6.3.1 Common requirements")) of os, except that: - [(4.1)](#print-4.1) failure to generate output is reported as specified below, and - [(4.2)](#print-4.2) any exception thrown by the call to vformat is propagated without regard to the value of os.exceptions() and without turning on ios_base​::​badbit in the error state of os[.](#print-4.sentence-1) After constructing a sentry object, the function initializes a variable with automatic storage duration viastring out = vformat(os.getloc(), fmt, args); - [(4.3)](#print-4.3) If the function is vprint_unicode andos is a stream that refers to a terminal that is capable of displaying Unicode only via a native Unicode API, which is determined in an implementation-defined manner, flushes os and then writes out to the terminal using the native Unicode API; if out contains invalid code units,the behavior is undefined[.](#print-4.3.sentence-1) Then establishes an observable checkpoint ([[intro.abstract]](intro.abstract "4.1.2 Abstract machine"))[.](#print-4.3.sentence-2) - [(4.4)](#print-4.4) Otherwise inserts the character sequence [out.begin(), out.end()) into os[.](#print-4.4.sentence-1) If writing to the terminal or inserting into os fails, calls os.setstate(ios_base​::​badbit) (which may throw ios_base​::​failure)[.](#print-4.sentence-3) [5](#print-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6902) *Recommended practice*: For vprint_unicode, if invoking the native Unicode API requires transcoding, implementations should substitute invalid code units with U+fffd replacement character per the Unicode Standard, Chapter 3.9 U+fffd Substitution in Conversion[.](#print-5.sentence-1)