Files
cppdraft_translate/cppdraft/format/tuple.md
2025-10-25 03:02:53 +03:00

6.2 KiB

[format.tuple]

28 Text processing library [text]

28.5 Formatting [format]

28.5.9 Tuple formatter [format.tuple]

1

#

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... Ts>struct formatter<pair-or-tuple<Ts...>, charT> {private: tuple<formatter<remove_cvref_t, charT>...> underlying_; // exposition only basic_string_view separator_ = STATICALLY-WIDEN(", "); // exposition only basic_string_view opening-bracket_ = STATICALLY-WIDEN("("); // exposition only basic_string_view closing-bracket_ = STATICALLY-WIDEN(")"); // exposition onlypublic:constexpr void set_separator(basic_string_view sep) noexcept; constexpr void set_brackets(basic_string_view opening, basic_string_view closing) noexcept; templateconstexpr typename ParseContext::iterator parse(ParseContext& ctx); templatetypename 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 && ...);}

2

#

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-alignopt widthopt tuple-typeopt

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

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

tuple-type :
m
n

3

#

The tuple-fill-and-align is interpreted the same way as a fill-and-align ([format.string.std]).

The productions align and width are described in [format.string].

4

#

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 116.

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

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

🔗

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

5

#

Effects: Equivalent to: separator_ = sep;

🔗

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

6

#

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

🔗

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

7

#

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

The values ofopening-bracket_,closing-bracket_, andseparator_ 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().

8

#

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

🔗

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

9

#

The type of elems is:

  • (9.1)

    If (formattable<const Ts, charT> && ...) is true,const pair-or-tuple<Ts...>&.

  • (9.2)

    Otherwise pair-or-tuple<Ts...>&.

10

#

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

opening-bracket_,

for each index I in the [0, sizeof...(Ts)):

if I != 0, separator_,

the result of writing get(elems) via get(underlying_), and

closing-bracket_.

11

#

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