C++ named requirements: Formatter

< cpp‎ | named req
C++ named requirements
Type properties
Container Elements
Stream I/O
Random Numbers


Formatter is a type that abstracts formatting operations for a given formatting argument type and character type. Specializations of std::formatter are required to model Formatter.

[edit] Requirements

A type satisfies Formatter if it is semiregular, meaning it satisfies:

And, given

  • Arg, a formatting argument type
  • CharT, a character type
  • Formatter, a Formatter type for types Arg and CharT
  • OutputIt, a LegacyOutputIterator type
  • formatter, a value of type Formatter
  • arg, an lvalue of type Arg
  • t, a value of type convertible to (possibly const) Arg
  • ParseContext, an alias of std::basic_format_parse_context<CharT>
  • FormatContext, an alias of std::basic_format_context<OutputIt, CharT>
  • parse_context, an lvalue of type ParseContext
  • format_context, an lvalue of type FormatContext
Expression Return type Semantics
formatter.parse(parse_context) ParseContext::iterator
  • Parses the format-spec [parse_context.begin(), parse_context.end()) for type Arg until the first unmatched character.
  • Throws std::format_error unless the whole range is parsed or the unmatched character is }. [note 1]
  • Stores the parsed format specifiers in formatter and returns an end iterator of the parsed range.
formatter.format(t, format_context) FormatContext::​iterator
  • Formats t according to the specifiers stored in formatter, writes the output to format_context.out() and returns an end iterator of the output range.
  • The output shall only depend on
    • t,
    • format_context.locale(),
    • the range [parse_context.begin(), parse_context.end()) from the last call to formatter.parse(parse_context), and
    • format_context.arg(n) for any value n of type std::size_t.
formatter.format(arg, format_context) FormatContext::​iterator

As above, but does not modify arg.

  1. This allows formatters to emit meaningful error messages.