28 Text processing library [text] (original) (raw)

28.5 Formatting [format]

28.5.9 Tuple formatter [format.tuple]

For each of pair and tuple, the library provides the following formatter specialization where pair-or-tuple is the name of the template:

namespace std { template<class charT, formattable<charT>... Ts> struct formatter<_pair-or-tuple_<Ts...>, charT> { private: tuple<formatter<remove_cvref_t<Ts>, charT>...> underlying_; basic_string_view<charT> separator_ = STATICALLY-WIDEN<charT>(", "); basic_string_view<charT> opening-bracket_ = STATICALLY-WIDEN<charT>("("); basic_string_view<charT> closing-bracket_ = STATICALLY-WIDEN<charT>(")"); public: constexpr void set_separator(basic_string_view<charT> sep) noexcept;constexpr void set_brackets(basic_string_view<charT> opening, basic_string_view<charT> closing) noexcept;template<class ParseContext> constexpr typename ParseContext::iterator parse(ParseContext& ctx);template<class FormatContext> typename FormatContext::iterator format(see below& elems, FormatContext& ctx) const;};template<class... Ts> constexpr bool enable_nonlocking_formatter_optimization<_pair-or-tuple_<Ts...>> = (enable_nonlocking_formatter_optimization<Ts> && ...);}

The parse member functions of these formatters interpret the format specification as a tuple-format-spec according to the following syntax:

tuple-format-spec :
tuple-fill-and-align width tuple-type

tuple-fill-and-align :
tuple-fill align

tuple-fill :
any character other than { or } or :

The tuple-type specifier changes the way a pair or tuple is formatted, with certain options only valid with certain argument types.

The meaning of the various type options is as specified in Table 114.

Table 114 — Meaning of tuple-type options [tab:formatter.tuple.type]

🔗Option Requirements Meaning
🔗m sizeof...(Ts) == 2 Equivalent to:set_separator(STATICALLY-WIDEN<charT>(": ")); set_brackets({}, {});
🔗n none Equivalent to: set_brackets({}, {});
🔗none none No effects

constexpr void set_separator(basic_string_view<charT> sep) noexcept;

Effects: Equivalent to: separator_ = sep;

constexpr void set_brackets(basic_string_view<charT> opening, basic_string_view<charT> closing) noexcept;

Effects: Equivalent to:opening-bracket_ = opening;closing-bracket_ = closing;

template<class ParseContext> constexpr typename ParseContext::iterator parse(ParseContext& ctx);

Effects: Parses the format specifiers as a tuple-format-spec and stores the parsed specifiers in *this.

The values of_opening-bracket__,closing-bracket_, and_separator__are modified if and only if required by the tuple-type, if present.

For each element e in underlying_, calls e.parse(ctx) to parse an empty format-spec and, if e.set_debug_format() is a valid expression, calls e.set_debug_format().

Returns: An iterator past the end of the tuple-format-spec.

template<class FormatContext> typename FormatContext::iterator format(_see below_& elems, FormatContext& ctx) const;

The type of elems is:

Effects: Writes the following into ctx.out(), adjusted according to the tuple-format-spec:

Returns: An iterator past the end of the output range.