[ostream.formatted] (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.3 Formatted output functions [ostream.formatted]
31.7.6.3.1 Common requirements [ostream.formatted.reqmts]
Each formatted output function begins execution by constructing an object of classsentry.
If that object returnstruewhen converted to a value of typebool, the function endeavors to generate the requested output.
If the generation fails, then the formatted output function doessetstate(ios_base::failbit), which can throw an exception.
If an exception is thrown during output, thenios_base::badbitis set286in*this's error state.
If(exceptions() & badbit) != 0then the exception is rethrown.
Whether or not an exception is thrown, thesentryobject is destroyed before leaving the formatted output function.
If no exception is thrown, the result of the formatted output function is*this.
The descriptions of the individual formatted output functions describe how they perform output and do not mention thesentryobject.
If a formatted output function of a stream os determines padding, it does so as follows.
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.
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.
31.7.6.3.2 Arithmetic inserters [ostream.inserters.arithmetic]
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);
Effects: The classesnum_get<>andnum_put<>handle locale-dependent numeric formatting and parsing.
These inserter functions use the imbuedlocalevalue to perform numeric formatting.
When valis 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<num_put<charT, ostreambuf_iterator<charT, traits>>>( getloc()).put(*this, *this, fill(), val).failed();
When val is of typeshortthe 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<num_put<charT, ostreambuf_iterator<charT, traits>>>( getloc()).put(*this, *this, fill(), baseflags == ios_base::oct || baseflags == ios_base::hex? static_cast<long>(static_cast<unsigned short>(val)) : static_cast<long>(val)).failed();
When val is of typeintthe 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<num_put<charT, ostreambuf_iterator<charT, traits>>>( getloc()).put(*this, *this, fill(), baseflags == ios_base::oct || baseflags == ios_base::hex? static_cast<long>(static_cast<unsigned int>(val)) : static_cast<long>(val)).failed();
When val is of typeunsigned shortorunsigned intthe formatting conversion occurs as if it performed the following code fragment:bool failed = use_facet<num_put<charT, ostreambuf_iterator<charT, traits>>>( getloc()).put(*this, *this, fill(), static_cast<unsigned long>(val)).failed();
When val is of typefloatthe formatting conversion occurs as if it performed the following code fragment:bool failed = use_facet<num_put<charT, ostreambuf_iterator<charT, traits>>>( getloc()).put(*this, *this, fill(), static_cast<double>(val)).failed();
The first argument provides an object of theostreambuf_iterator<>class which is an iterator for class basic_ostream<>.
It bypassesostreams and usesstreambufs directly.
Classlocalerelies on these types as its interface to iostreams, since for flexibility it has been abstracted away from direct dependence onostream.
The second parameter is a reference to the base class subobject of typeios_base.
It provides formatting specifications such as field width, and a locale from which to obtain other facets.
Iffailedistruethen doessetstate(badbit), which may throw an exception, and returns.
basic_ostream& operator<<(const volatile void* p);
Effects: Equivalent to: return operator<<(const_cast<const void*>(p));
basic_ostream& operator<<(_extended-floating-point-type_ val);
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<num_put<charT, ostreambuf_iterator<charT, traits>>>( getloc()).put(*this, *this, fill(), static_cast<double>(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<num_put<charT, ostreambuf_iterator<charT, traits>>>( getloc()).put(*this, *this, fill(), static_cast<long double>(val)).failed();
Otherwise, an invocation of the operator function is conditionally supported with implementation-defined semantics.
If failed is true then does setstate(badbit), which may throw an exception, and returns.
31.7.6.3.3 basic_ostream::operator<< [ostream.inserters]
basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));
Effects: None.
Does not behave as a formatted output function (as described in [ostream.formatted.reqmts]).
basic_ostream& operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
Effects: Callspf(*this).
This inserter does not behave as a formatted output function (as described in [ostream.formatted.reqmts]).
basic_ostream& operator<<(ios_base& (*pf)(ios_base&));
Effects: Callspf(*this).
This inserter does not behave as a formatted output function (as described in [ostream.formatted.reqmts]).
basic_ostream& operator<<(basic_streambuf<charT, traits>* sb);
After the sentry object is constructed, ifsb is null callssetstate(badbit)(which may throwios_base::failure).
Gets characters from sband inserts them in*this.
Characters are read from sband inserted until any of the following occurs:
- end-of-file occurs on the input sequence;
- inserting in the output sequence fails (in which case the character to be inserted is not extracted);
- an exception occurs while getting a character from sb.
If the function inserts no characters, it callssetstate(failbit)(which may throwios_base::failure ([iostate.flags])).
If an exception was thrown while extracting a character, the function setsfailbitin the error state, and iffailbitis set inexceptions()the caught exception is rethrown.
basic_ostream& operator<<(nullptr_t);
Effects: Equivalent to:return *this << s;where s is animplementation-definedNTCTS.
31.7.6.3.4 Character inserter function templates [ostream.inserters.character]
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& out, charT c);template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& out, char c);// specialization template<class traits> basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, char c);// signed and unsigned template<class traits> basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, signed char c);template<class traits> basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, unsigned char c);
Effects: Behaves as a formatted output functionof out.
Constructs a character sequence seq.
If c has typecharand the character container type of the stream is notchar, then seq consists ofout.widen(c); otherwise seq consists ofc.
Determines padding for seq as described in [ostream.formatted.reqmts].
Inserts seq intoout.
Calls os.width(0).
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& out, const charT* s);template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& out, const char* s);template<class traits> basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, const char* s);template<class traits> basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, const signed char* s);template<class traits> basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out,const unsigned char* s);
Preconditions: s is not a null pointer.
Effects: Behaves like a formatted inserter (as described in [ostream.formatted.reqmts]) of out.
Creates a character sequence seq of n characters starting at s, each widened usingout.widen() ([basic.ios.members]), where n is the number that would be computed as if by:
- traits::length(s)for the overload where the first argument is of typebasic_ostream<charT, traits>&and the second is of typeconst charT*, and also for the overload where the first argument is of typebasic_ostream<char, traits>&and the second is of typeconst char*,
- char_traits<char>::length(s)for the overload where the first argument is of typebasic_ostream<charT, traits>&and the second is of typeconst char*,
- traits::length(reinterpret_cast<const char*>(s))for the other two overloads.
Determines padding for seq as described in [ostream.formatted.reqmts].
Inserts seq intoout.
Calls width(0).
31.7.6.3.5 Print [ostream.formatted.print]
template<class... Args> void print(ostream& os, format_string<Args...> fmt, Args&&... args);
Effects: If the ordinary literal encoding ([lex.charset]) 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...));
template<class... Args> void println(ostream& os, format_string<Args...> fmt, Args&&... args);
Effects: Equivalent to:print(os, "{}\n", format(os.getloc(), fmt, std::forward<Args>(args)...));
void println(ostream& os);
Effects: Equivalent to:print(os, "\n");
void vprint_unicode(ostream& os, string_view fmt, format_args args);void vprint_nonunicode(ostream& os, string_view fmt, format_args args);
Effects: Behaves as a formatted output function ([ostream.formatted.reqmts]) of os, except that:
- failure to generate output is reported as specified below, and
- 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.
After constructing a sentry object, the function initializes a variable with automatic storage duration viastring out = vformat(os.getloc(), fmt, args);
- 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.
- Otherwise inserts the character sequence [out.begin(), out.end()) into os.
If writing to the terminal or inserting into os fails, calls os.setstate(ios_base::badbit)(which may throw ios_base::failure).
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.