std::formatter<pair-or-tuple>
| 定义于头文件 <format> |
||
| template< class CharT, std::formattable<CharT>... Ts > struct formatter</*pair-or-tuple*/<Ts...>, CharT>; |
(C++23 起) | |
std::formatter 对 std::pair 和 std::tuple 的模板特化允许用户使用格式化函数将 pair 或 tuple 转换为其元素集合的文本表示。
仅用于解释的名称 /*pair-or-tuple*/ 表示类模板 std::pair 或 std::tuple。
如果 (std::formattable<const Ts, CharT> && ...) 为 true,则此特化满足 Formatter 要求。它总是满足 BasicFormatter 要求。
目录 |
[编辑] 格式规范
tuple-format-spec 的语法是
| tuple-fill-and-align (可选) width (可选) tuple-type (可选) | |||||||||
tuple-fill-and-align 的解释方式与 fill-and-align 相同,只是 tuple-fill-and-align 中的 fill 是除了 {、} 或 : 之外的任何字符。
width 在标准格式宽度规范中描述。
tuple-type 改变了元组的格式化方式,某些选项仅对某些参数类型有效。
可用的元组表示类型有
-
m:表示开括号和闭括号都应为 "",而分隔符应为 ": "。
- 如果选择
m作为 tuple-type,则除非 sizeof...(Ts) == 2 为 true,否则程序是非良构的。
- 如果选择
-
n:表示分隔符、开括号和闭括号都应为 ""。
[编辑] 成员对象
| 成员名称 (Member name) | 定义 |
underlying_ (私有) |
类型为 std::tuple<std::formatter<std::remove_cvref_t<Ts>, CharT>...> 的底层格式化器元组 (仅用于阐释的成员对象*) |
separator_ (私有) |
表示元组格式化结果分隔符的字符串(默认为 ", ") (仅用于阐释的成员对象*) |
opening-bracket_ (私有) |
表示元组格式化结果开括号的字符串(默认为 "(") (仅用于阐释的成员对象*) |
closing-bracket_ (私有) |
表示元组格式化结果闭括号的字符串(默认为 ")") (仅用于阐释的成员对象*) |
[编辑] 成员函数
| set_separator |
为元组格式化结果设置指定的分隔符 (公开成员函数) |
| set_brackets |
为元组格式化结果设置指定的开括号和闭括号 (公开成员函数) |
| parse |
按 tuple-format-spec 指定的方式解析格式说明符 (公开成员函数) |
| format |
按 tuple-format-spec 指定的方式写入元组格式化输出 (公开成员函数) |
std::formatter<pair-or-tuple>::set_separator
| constexpr void set_separator( std::basic_string_view<CharT> sep ) noexcept; |
||
将 sep 赋值给 separator_。
std::formatter<pair-or-tuple>::set_brackets
| constexpr void set_brackets( std::basic_string_view<CharT> opening, std::basic_string_view<CharT> closing ) noexcept; |
||
分别将 opening 和 closing 赋值给 opening-bracket_ 和 closing-bracket_。
std::formatter<pair-or-tuple>::parse
| template< class ParseContext > constexpr auto parse( ParseContext& ctx ) -> ParseContext::iterator; |
||
将格式说明符解析为 tuple-format-spec 并将解析后的说明符存储到当前对象中。
如果存在 tuple-type 或 n 选项,则根据需要修改 opening-bracket、closing-bracket 和 separator 的值。
对于 underlying_ 中的每个元素 e,调用 e.parse(ctx) 来解析空的 format-spec,如果 e.set_debug_format() 是一个有效的表达式,则调用 e.set_debug_format()。
返回一个迭代器,指向 tuple-format-spec 的末尾之后。
std::formatter<pair-or-tuple>::format
| template< class FormatContext > FormatContext::iterator |
||
/*maybe-const-pair-or-tuple*/ 表示
- const /*pair-or-tuple*/,如果 (std::formattable<const Ts, CharT> && ...) 为 true,
- 否则为 /*pair-or-tuple*/。
按 tuple-format-spec 指定的顺序将以下内容写入 ctx.out()
-
opening-bracket_, - 对于
[0,sizeof...(Ts))中的每个索引 I
- 如果 I != 0,则为
separator_, - 通过 std::get<I>(
underlying_) 写入 std::get<I>(elems) 的结果,以及
- 如果 I != 0,则为
-
closing-bracket_.
返回一个迭代器,指向输出范围的末尾之后。
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
|---|---|---|---|
| LWG 3892 | C++23 | 嵌套元组的格式化不正确 | 已更正 |
[编辑] 另请参阅
| (C++20) |
定义给定类型的格式化规则 (类模板) |