std::basic_format_string, std::format_string, std::wformat_string
来自cppreference.com
| 在标头 <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::runtime_format 返回的,否则 (C++26 起)std::basic_format_string 的构造函数执行编译时格式字符串检查。
成员函数
| (构造函数) |
构造 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( /* 运行时格式字符串 */<CharT> s ) noexcept; |
(2) | (C++26 起) |
1) 构造
basic_format_string 对象,用于存储字符串 s 的视图。如果实参不是编译时常量,或者无法解析为针对格式化参数类型 Args 的格式字符串,则构造非良构。 此重载只有在 const T& 实现 std::convertible_to<std::basic_string_view<CharT>> 时才会参与重载决议。
2) 构造
basic_format_string 对象,用于存储 std::runtime_format 返回的字符串 s 的视图。不会在构造时执行格式字符串检查。参数
| 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 的名字
|