Namespaces
Variants

std:: vformat_to

From cppreference.net
定义于头文件 <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 ,则将其用于区域设置特定的格式化。

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

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

OutputIt 必须满足 std:: output_iterator < const CharT & > 概念(符合其语义要求),且对于参数类型中的任意 Ti std:: formatter < Ti, CharT > 必须满足 Formatter 要求。否则行为未定义。

目录

参数

out - 输出缓冲区的迭代器
fmt - 表示格式字符串的对象。格式字符串包含:
  • 普通字符(除 { } 外),它们会原样复制到输出,
  • 转义序列 { { } } ,它们在输出中分别被替换为 { } ,以及
  • 替换字段。

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

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

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

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

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

返回值

输出范围末尾之后的迭代器。

异常

fmt 不是有效格式字符串时抛出 std::format_error 。同时会传播由格式化器或迭代器操作抛出的任何异常。

示例

缺陷报告

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

缺陷报告 适用范围 发布时行为 正确行为
P2216R3 C++20 args 的类型基于 OutputIt 参数化 不进行参数化

参见