std:: basic_format_string, std:: format_string, std:: wformat_string
|
定义于头文件
<format>
|
||
|
template
<
class
CharT,
class
...
Args
>
struct basic_format_string ; |
(1) | (C++20 起) |
|
template
<
class
...
Args
>
using
format_string
=
|
(2) | (C++20 起) |
|
template
<
class
...
Args
>
using
wformat_string
=
|
(3) | (C++20 起) |
类模板
std::basic_format_string
封装了一个将被格式化函数使用的
std::basic_string_view
。
std::basic_format_string
的构造函数会执行编译期格式字符串检查
,除非构造函数参数是通过
std::runtime_format
返回的
(C++26 起)
。
目录 |
成员函数
|
(constructor)
|
构造
basic_format_string
,若参数不是格式化字符串则引发编译错误
(公开成员函数) |
|
get
|
返回被包装的字符串
(公开成员函数) |
std::basic_format_string:: basic_format_string
|
template
<
class
T
>
consteval basic_format_string ( const T & s ) ; |
(1) | |
|
basic_format_string
(
/* runtime-format-string */
<
CharT
>
s
)
noexcept
;
|
(2) | (since C++26) |
basic_format_string
对象。如果参数不是编译时常量,或者无法将其解析为格式化参数类型
Args
的格式字符串,则构造格式不正确。
basic_format_string
对象。构造时不执行格式字符串检查。
参数
| s | - |
表示格式字符串的对象。格式字符串包含:
每个替换字段具有以下格式:
1)
不带格式说明的替换字段
2)
带格式说明的替换字段
|
||||||||||||||||||||||||||||||||||||||||||||||
std::basic_format_string:: get
|
constexpr
std::
basic_string_view
<
CharT
>
get
(
)
const
noexcept
;
|
||
返回存储的字符串视图。
注释
别名模板
format_string
和
wformat_string
使用
std::type_identity_t
来抑制模板参数推导。通常,当它们作为函数参数出现时,其模板参数会从其他函数参数中推导得出。
template<class... Args> std::string format(std::format_string<Args...> fmt, Args&&... args); auto s = format("{} {}", 1.0, 2); // 调用 format<double, int>。Args 从 1.0, 2 推导得出 // 由于 format_string 中使用 type_identity_t,模板参数推导 // 不会考虑格式化字符串的类型
示例
|
本节内容不完整
原因:缺少示例 |
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| P2508R1 | C++20 | 该功能没有用户可见的名称 |
公开了
basic_format_string
名称
|