命名空间
变体
操作

std::vformat_to

来自 cppreference.cn
< cpp‎ | 工具‎ | 格式化
 
 
 
 
定义于头文件 <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++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
P2216R3 C++20 args 的类型根据 OutputIt 进行参数化 不参数化

[编辑] 参见