“cpp/utility/format/basic format string”的版本间的差异
来自cppreference.com
YexuanXiao(讨论 | 贡献) (以“{{cpp/title|basic_format_string|format_string|wformat_string}} {{cpp/utility/format/navbar}} {{dcl begin}} {{dcl header | format}} {{dcl | num=1 | since=c++20 |1= t...”为内容创建页面) |
YexuanXiao(讨论 | 贡献) 小 |
||
第24行: | 第24行: | ||
===成员函数=== | ===成员函数=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc mem ctor | nolink=true | 构造 | + | {{dsc mem ctor | nolink=true | 构造 {{tt|basic_format_string}},当参数不是一个格式化字符串时引起编译错误}} |
{{dsc mem fun | nolink=true | get | 返回包装的字符串}} | {{dsc mem fun | nolink=true | get | 返回包装的字符串}} | ||
{{dsc end}} | {{dsc end}} | ||
第41行: | 第41行: | ||
@1@ 构造 {{tt|basic_format_string}} 对象,用于存储字符串 {{tt|s}} 的视图。如果参数不是编译时常量,或者无法解析为格式化参数类型 {{tt|Args}} 的格式字符串,则构造失败。 | @1@ 构造 {{tt|basic_format_string}} 对象,用于存储字符串 {{tt|s}} 的视图。如果参数不是编译时常量,或者无法解析为格式化参数类型 {{tt|Args}} 的格式字符串,则构造失败。 | ||
− | :* {{cpp/enable if|{{c|const T&}} | + | :* {{cpp/enable if|{{c|const T&}} {{c|std::convertible_to<std::basic_string_view<CharT>>}}。}} |
@2@ 构造 {{tt|basic_format_string}} 对象,用于存储 {{lc|std::runtime_format}} 返回的字符串 {{tt|s}} 的视图。不会执行格式字符串检查。 | @2@ 构造 {{tt|basic_format_string}} 对象,用于存储 {{lc|std::runtime_format}} 返回的字符串 {{tt|s}} 的视图。不会执行格式字符串检查。 |
2024年1月25日 (四) 19:27的版本
在标头 <format> 定义
|
||
template< class CharT, class... Args > struct basic_format_string; |
(1) | (C++20 起) |
template< class... Args > using format_string = basic_format_string<char, std::type_identity_t<Args>...>; |
(2) | (C++20 起) |
template< class... Args > using wformat_string = basic_format_string<wchar_t, std::type_identity_t<Args>...>; |
(3) | (C++20 起) |
类模板 std::basic_format_string
包装一个用于格式化函数的 std::basic_string_view。
它在构造时如果构造函数参数不是从 std::runtime_format 返回(C++26 起)执行编译时格式化字符串检查。
目录 |
成员函数
(构造函数) |
构造 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) | (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); // Calls format<double, int>. Args are deduced from 1.0, 2 // Due to the use of type_identity_t in format_string, template argument deduction // does not consider the type of the format string.
示例
本节未完成 原因:暂无示例 |
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
P2508R1 | C++20 | 这个设施没有用户可见的名字 | 暴露出 basic_format_string 的名字
|