Skip to main content

C++ named requirements: FormattedOutputFunction

Requirements

A FormattedOutputFunction is a stream output function that performs the following:

  • Constructs an object of type basic_istream::sentry with automatic storage duration, which performs the following
    • if eofbit or badbit are set on the output stream, sets the failbit as well, and if exceptions on failbit are enabled in this output stream's exception mask ((exceptions() & failbit) != 0), throws ios_base::failure.
    • flushes the tie()'d output stream, if applicable.
  • Checks the status of the sentry by calling sentry::operator bool(), which is equivalent to basic_ios::good.
  • If the sentry returned false or sentry's constructor threw an exception, no output takes place
  • If the sentry returned true, attempts to perform the desired output by inserting the characters into the output stream as if by calling rdbuf()->sputc(). Other public members of std::basic_ostream may also be used, but virtual members of rdbuf() except overflow(), xsputn() and sync() will never be called.
    • If the output could not be generated, sets failbit. If exceptions on failbit are enabled in this stream's exception mask ((exceptions() & failbit) != 0), throws ios_base::failure.
    • if an exception is thrown during output, sets badbit in the output stream. If exceptions on badbit are enabled in this stream's exception mask ((exceptions() & failbit) != 0), the exception is also rethrown.
    • If no exception was thrown, returns *this.
  • In any event, whether terminating by exception or returning, the sentry's destructor is called before leaving this function.

Padding

Formatted output functions determine padding according to std::num_put::do_put() stage 3.

 (until C++14)

If a formatted output function of a stream os determines padding, it does so as follows.
Given a CharT character sequence seq where CharT is the character type of os, if the length of seq is less than os.width(), then enough copies of os.fill() are added to this sequence as necessary to pad to a width of os.width() characters.
If (os.flags() & std::ios_base::adjustfield) == std::ios_base::left is true, the fill characters are placed after the character sequence; otherwise, they are placed before the character sequence.

 (since C++14)

Standard library

The following standard library functions are FormattedOutputFunctions.

 (since C++23)

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 160C++98the process of determining whether the exception caught is rethrown mentioned a non-existing function exception()corrected to exceptions()
LWG 165C++98the only virtual member allowed to be called on rdbuf() was overflow()also allowed xsputn() and sync()

C++ named requirements: FormattedOutputFunction

Requirements

A FormattedOutputFunction is a stream output function that performs the following:

  • Constructs an object of type basic_istream::sentry with automatic storage duration, which performs the following
    • if eofbit or badbit are set on the output stream, sets the failbit as well, and if exceptions on failbit are enabled in this output stream's exception mask ((exceptions() & failbit) != 0), throws ios_base::failure.
    • flushes the tie()'d output stream, if applicable.
  • Checks the status of the sentry by calling sentry::operator bool(), which is equivalent to basic_ios::good.
  • If the sentry returned false or sentry's constructor threw an exception, no output takes place
  • If the sentry returned true, attempts to perform the desired output by inserting the characters into the output stream as if by calling rdbuf()->sputc(). Other public members of std::basic_ostream may also be used, but virtual members of rdbuf() except overflow(), xsputn() and sync() will never be called.
    • If the output could not be generated, sets failbit. If exceptions on failbit are enabled in this stream's exception mask ((exceptions() & failbit) != 0), throws ios_base::failure.
    • if an exception is thrown during output, sets badbit in the output stream. If exceptions on badbit are enabled in this stream's exception mask ((exceptions() & failbit) != 0), the exception is also rethrown.
    • If no exception was thrown, returns *this.
  • In any event, whether terminating by exception or returning, the sentry's destructor is called before leaving this function.

Padding

Formatted output functions determine padding according to std::num_put::do_put() stage 3.

 (until C++14)

If a formatted output function of a stream os determines padding, it does so as follows.
Given a CharT character sequence seq where CharT is the character type of os, if the length of seq is less than os.width(), then enough copies of os.fill() are added to this sequence as necessary to pad to a width of os.width() characters.
If (os.flags() & std::ios_base::adjustfield) == std::ios_base::left is true, the fill characters are placed after the character sequence; otherwise, they are placed before the character sequence.

 (since C++14)

Standard library

The following standard library functions are FormattedOutputFunctions.

 (since C++23)

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 160C++98the process of determining whether the exception caught is rethrown mentioned a non-existing function exception()corrected to exceptions()
LWG 165C++98the only virtual member allowed to be called on rdbuf() was overflow()also allowed xsputn() and sync()