std::format
来自cppreference.com
| 在标头 <format> 定义
|
||
| template< class... Args > std::string format( std::format_string<Args...> fmt, Args&&... args ); |
(1) | (C++20 起) |
| template< class... Args > std::wstring format( std::wformat_string<Args...> fmt, Args&&... args ); |
(2) | (C++20 起) |
| template< class... Args > std::string format( const std::locale& loc, |
(3) | (C++20 起) |
| template< class... Args > std::wstring format( const std::locale& loc, |
(4) | (C++20 起) |
按照格式字符串 fmt 格式化 args,并返回作为字符串的结果。若存在 loc,则它用于本地环境特定的格式化。
1) 等价于 return std::vformat(fmt.get(), std::make_format_args(args...));。
2) 等价于 return std::vformat(fmt.get(), std::make_wformat_args(args...));。
3) 等价于 return std::vformat(loc, fmt.get(), std::make_format_args(args...));。
4) 等价于 return std::vformat(loc, fmt.get(), std::make_wformat_args(args...));。
于编译时检查格式字符串 fmt,除非它是从 std::runtime_format 的返回结果初始化的(C++26 起)。如果在编译时发现格式字符串对于要格式化的实参类型无效,则会发出编译错误。
以下要求适用于 Args 中的每个类型 T,其中 CharT 对于重载 (1,3) 是 char,对于重载 (2,4) 是 wchar_t:
- std::formatter<T, CharT> 必须满足 基本格式化器 (BasicFormatter)
- std::formatter<T, CharT>::parse() 为进行编译时格式字符串检查必须为 constexpr
参数
| fmt | - |
每个替换域拥有如下格式:
1) 没有格式说明的替换域
2) 有格式说明的替换域
| ||||||||||||||||||||||||||||||||||||||||||||||
| args... | - | 要格式化的实参 | ||||||||||||||||||||||||||||||||||||||||||||||
| loc | - | 用于本地环境特定格式化的 std::locale | ||||||||||||||||||||||||||||||||||||||||||||||
返回值
保有格式化结果的字符串对象。
异常
在分配失败时抛出 std::bad_alloc。并且会传播格式化器所抛的任何异常。
注解
提供多于格式字符串所要求的实参不是错误:
std::format("{} {}!", "Hello", "world", "something"); // OK,产生 "Hello world!"
不是常量表达式的格式字符串,除非它是从 std::runtime_format 的返回结果初始化的,否则(C++26 起)是错误的。std::vformat 不要求这点。
std::string f1(std::string_view runtime_format_string) { // return std::format(runtime_format_string, "x", 42); // 错误 char v1[] = "x"; int v2 = 42 return std::vformat(runtime_format_string, std::make_format_args(v1, v2)); // OK } std::string f2(std::string_view runtime_format_string) { return std::format(std::runtime_format(runtime_format_string), "x", 42); // OK (C++26) }
示例
运行此代码
#include <format> #include <iostream> #include <string> #include <string_view> template <typename... Args> std::string dyna_print(std::string_view rt_fmt_str, Args&&... args) { return std::vformat(rt_fmt_str, std::make_format_args(args...)); } int main() { #ifdef __cpp_lib_format_ranges const std::set<std::string_view> continents { "Africa", "America", "Antarctica", }; std::cout << std::format("Hello {}!\n", continents); #else std::cout << std::format("Hello {}!\n", "continents"); #endif std::string fmt; for (int i{}; i != 3; ++i) { fmt += "{} "; // 构造格式化字符串 std::cout << fmt << " : "; std::cout << dyna_print(fmt, "alpha", 'Z', 3.14, "unused"); std::cout << '\n'; } }
可能的输出:
Hello {"Africa", "America", "Antarctica", "Asia", "Australia", "Europe"}!
{} : alpha
{} {} : alpha Z
{} {} {} : alpha Z 3.14缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| P2216R3 | C++20 | 对非法格式字符串抛出 std::format_error | 非法格式字符串导致编译时错误 |
| P2418R2 | C++20 | 既不可常量使用也不可复制的对象 (例如生成器类对象)不可格式化 |
允许格式化这些对象 |
| P2508R1 | C++20 | 这个设施没有用户可见的名字 | 暴露出 basic_format_string 的名字
|
参阅
| (C++20) |
通过输出迭代器写入其实参的格式化表示 (函数模板) |
| (C++20) |
通过输出迭代器写入其实参的格式化表示,不超出指定的大小 (函数模板) |