std:: make_format_args, std:: make_wformat_args
|
定义于头文件
<format>
|
||
|
template
<
class
Context
=
std::
format_context
,
class
...
Args
>
/*format-arg-store*/
<
Context, Args...
>
|
(1) | (C++20 起) |
|
template
<
class
...
Args
>
/*format-arg-store*/
<
std::
wformat_context
, Args...
>
|
(2) | (C++20 起) |
返回一个存储格式化参数数组的对象,该对象可隐式转换为 std::basic_format_args<Context> 。
如果对于
Args
中的任意
Ti
,
typename
Context
::
template
formatter_type
<
std::
remove_const_t
<
Ti
>>
不满足
BasicFormatter
要求,则行为未定义。
如果对于
Args
中的任何类型
Ti
,
Ti
不满足
__formattable_with
<
Context
>
,则程序非良构。
目录 |
参数
| args... | - | 用作格式化参数的值 |
返回值
一个保存格式化参数的对象。
对于每个类型为
T
的参数
t
,令
TD
为
std::
remove_const_t
<
std::
remove_reference_t
<
T
>>
。结果中对应的
std::basic_format_arg
按以下方式确定:
-
若
TD为 bool 或Context::char_type,则 std::basic_format_arg 存储 t ; -
否则,若
TD为 char 且Context::char_type为 wchar_t ,则 std::basic_format_arg 存储 static_cast < wchar_t > ( static_cast < unsigned char > ( t ) ) ; -
否则,若
TD为有符号整数类型且其大小不大于 int ,则 std::basic_format_arg 存储 static_cast < int > ( t ) ; -
否则,若
TD为无符号整数类型且其大小不大于 unsigned int ,则 std::basic_format_arg 存储 static_cast < unsigned int > ( t ) ; -
否则,若
TD为有符号整数类型且其大小不大于 long long ,则 std::basic_format_arg 存储 static_cast < long long > ( t ) ; -
否则,若
TD为无符号整数类型且其大小不大于 unsigned long long ,则 std::basic_format_arg 存储 static_cast < unsigned long long > ( t ) ; -
否则,若
TD为 float 、 double 或 long double ,则 std::basic_format_arg 存储 t ; -
否则,若
TD为 std::basic_string_view 或 std::basic_string 特化且TD::char_type与Context::char_type相同,则 std::basic_format_arg 存储 std:: basic_string_view < Context :: char_type > ( t. data ( ) , t. size ( ) ) ; - 否则,若 std:: decay_t < TD > 为 Context :: char_type * 或 const Context :: char_type * ,则 std::basic_format_arg 存储 static_cast < const Context :: char_type * > ( t ) ;
- 否则,若 std:: is_void_v < std:: remove_pointer_t < TD >> 为 true 或 std:: is_null_pointer_v < TD > 为 true ,则 std::basic_format_arg 存储 static_cast < const void * > ( t ) ;
-
否则,
std::basic_format_arg
存储一个指向
t的 std:: basic_format_arg < Context > :: handle ,以及handle::format()所需的额外数据。
注释
对于用户定义类型,格式化参数具有引用语义且不会延长 args 的生命周期。程序员有责任确保 args 的存活时间超过返回值。通常,结果仅用作格式化函数的参数。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_format_uchar
|
202311L
|
(C++20)
(DR) |
将代码单元格式化为无符号整数 |
示例
#include <array> #include <format> #include <iostream> #include <string_view> void raw_write_to_log(std::string_view users_fmt, std::format_args&& args) { static int n{}; std::clog << std::format("{:04} : ", n++) << std::vformat(users_fmt, args) << '\n'; } template<typename... Args> constexpr void log(Args&&... args) { // 生成格式化字符串 "{} "... std::array<char, sizeof...(Args) * 3 + 1> braces{}; constexpr const char c[4] = "{} "; for (auto i{0uz}; i != braces.size() - 1; ++i) braces[i] = c[i % 3]; braces.back() = '\0'; raw_write_to_log(std::string_view{braces.data()}, std::make_format_args(args...)); } template<typename T> const T& unmove(T&& x) { return x; } int main() { log("Number", "of", "arguments", "is", "arbitrary."); log("Any type that meets the BasicFormatter requirements", "can be printed."); log("For example:", 1, 2.0, '3', "*42*"); raw_write_to_log("{:02} │ {} │ {} │ {}", std::make_format_args(unmove(1), unmove(2.0), unmove('3'), "4")); }
输出:
0000 : Number of arguments is arbitrary. 0001 : Any type that meets the BasicFormatter requirements can be printed. 0002 : For example: 1 2.0 3 *42* 0003 : 01 │ 2.0 │ 3 │ 4
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| DR | 适用范围 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| P2418R2 | C++20 |
既不可常量使用也不可复制的对象
(如生成器类对象)不可格式化 |
允许格式化这些对象 |
| P2905R2 | C++20 |
make_format_args
通过转发引用接受右值参数
|
仅接受左值引用 |
| P2909R4 | C++20 |
char
或
wchar_t
可能被格式化为
超出范围的无符号整数值 |
代码单元在格式化前转换为对应的
无符号类型 |
| LWG 3631 | C++20 | P2418R2 后对 cv 限定参数的处理不正确 | 已修正处理方式 |
参见
|
(C++20)
(C++20)
(C++20)
|
提供对所有格式化参数访问的类
(类模板) |
|
(C++20)
|
使用类型擦除参数表示的
std::format
非模板变体
(函数) |
|
(C++20)
|
使用类型擦除参数表示的
std::format_to
非模板变体
(函数模板) |