命名空间
变体
操作

std::vformat_to

来自 cppreference.cn
< cpp‎ | utility‎ | format
 
 
 
 
定义于头文件 <format>
template< class OutputIt >
OutputIt vformat_to( OutputIt out, std::string_view fmt, std::format_args args );
(1) (C++20 起)
template< class OutputIt >
OutputIt vformat_to( OutputIt out, std::wstring_view fmt, std::wformat_args args );
(2) (C++20 起)
template< class OutputIt >

OutputIt vformat_to( OutputIt out, const std::locale& loc,

                     std::string_view fmt, std::format_args args );
(3) (C++20 起)
template< class OutputIt >

OutputIt vformat_to( OutputIt out, const std::locale& loc,

                     std::wstring_view fmt, std::wformat_args args );
(4) (C++20 起)

根据格式字符串 fmt 格式化 args 中保存的格式化参数,并将结果写入输出迭代器 out。如果存在 loc,则将其用于特定于区域设置的格式化。

CharTdecltype(fmt)::char_type (对于重载 (1,3)char,对于重载 (2,4)wchar_t)。

只有当 OutputIt 满足概念 std::output_iterator<const CharT&> 时,这些重载才会参与重载决议。

OutputIt 必须建模(满足语义要求)概念 std::output_iterator<const CharT&>,并且 std::formatter<Ti, CharT> 必须满足任何参数类型 TiFormatter 要求。否则,行为未定义。

内容

[编辑] 参数

out - 输出缓冲区的迭代器
fmt - 表示格式字符串的对象。格式字符串由以下内容组成:
  • 普通字符(除了 {}),它们被不变地复制到输出中,
  • 转义序列 {{}},它们在输出中分别被替换为 {},以及
  • 替换字段。

每个替换字段具有以下格式

{ arg-id (可选) } (1)
{ arg-id (可选) : format-spec } (2)
1) 没有格式规范的替换字段
2) 带有格式规范的替换字段
arg-id - 指定 args 中参数的索引,其值将用于格式化;如果省略,则按顺序使用参数。

格式字符串中的 arg-id 必须全部存在或全部省略。混合手动和自动索引是错误的。

format-spec - std::formatter 针对相应参数的特化定义的格式规范。不能以 } 开头。

(C++23 起)
(C++26 起)
  • 对于其他可格式化类型,格式规范由用户定义的 formatter 特化决定。
args - 要格式化的参数
loc - 用于特定于区域设置的格式化的 std::locale

[编辑] 返回值

超出输出范围末端的迭代器。

[编辑] 异常

如果 fmt 不是提供参数的有效格式字符串,则抛出 std::format_error。 也会传播格式化程序或迭代器操作抛出的任何异常。

[编辑] 示例

[编辑] 缺陷报告

以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
P2216R3 C++20 args 的类型在 OutputIt 上参数化 未参数化

[编辑] 参见