std::vprint_unicode, std::vprint_unicode_buffered
来自 cppreference.cn
定义于头文件 <print> |
||
void vprint_unicode( std::FILE* stream, std::string_view fmt, std::format_args args ); |
(1) | (C++23 起) |
void vprint_unicode_buffered( std::FILE* stream, std::string_view fmt, std::format_args args ); |
(2) | (C++23 起) |
void vprint_unicode_buffered( std::string_view fmt, std::format_args args ); |
(3) | (C++23 起) |
根据格式字符串 fmt 格式化 args,并将结果写入输出流。
1) 按顺序执行以下操作:
- 锁定 stream。
- 令 out 表示由 args 提供的格式化参数根据 fmt 中给出的规范进行格式化后的字符表示。
- 将 out 写入 stream。
- 如果 stream 指的是一个只能通过原生 Unicode API 显示 Unicode 的终端,则刷新 stream 并使用原生 Unicode API 将 out 写入终端。
- 否则,将未修改的 out 写入 stream。
函数退出时无条件解锁 stream。
如果满足以下任何条件,则行为未定义:
- stream 不是指向输出 C 流的有效指针。
- 使用原生 Unicode API 时,out 包含无效的 Unicode 代码单元。
2) 等价于 std::string out = std::vformat(fmt, args);
std::vprint_unicode(stream, "{}", std::make_format_args(out));。
std::vprint_unicode(stream, "{}", std::make_format_args(out));。
3) 等价于 std::vprint_unicode_buffered(stdout, fmt, args)。
将字符写入输出流后,建立一个可观察的检查点。 |
(C++26 起) |
目录 |
[编辑] 参数
stream | - | 要写入的输出文件流 | ||||||||||||||||||||||||||||||||||||||||||||||
fmt | - |
每个替换字段具有以下格式:
1) 没有格式化规范的替换字段
2) 带有格式化规范的替换字段
| ||||||||||||||||||||||||||||||||||||||||||||||
args | - | 要格式化的参数 |
[编辑] 异常
- 分配失败时抛出 std::bad_alloc。
- 如果写入流失败,抛出 std::system_error。
- 传播所使用的 formatters 抛出的任何异常,例如 std::format_error。
[编辑] 注意
C++ 标准鼓励实现者在 out 包含无效的 Unicode 代码单元时生成诊断消息。
在 POSIX 上,写入终端使用通常的标准 I/O 函数完成,因此不需要将终端与任何其他文件流区别对待。
在 Windows 上,如果 GetConsoleMode(_get_osfhandle(_fileno(stream))) 返回非零(请参阅 Windows 文档中的 GetConsoleMode
、_get_osfhandle
和 _fileno
),则流指向终端。Windows 上的原生 Unicode API 是 WriteConsoleW
。
如果调用原生 Unicode API 需要转码,则无效的代码单元将替换为 U+FFFD
REPLACEMENT CHARACTER(参见《Unicode 标准 - 核心规范》,第 3.9 章)。
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_print |
202207L |
(C++23) | 格式化输出 |
202403L |
(C++26) (DR23) |
无缓冲格式化输出 | |
202406L |
(C++26) (DR23) |
为更多可格式化类型启用无缓冲格式化输出 | |
__cpp_lib_format |
202207L |
(C++23) | 公开 std::basic_format_string |
[编辑] 示例
本节不完整 原因:无示例 |
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 4044 | C++23 | 如果 stream 引用的终端可以显示 Unicode,则始终使用原生 Unicode API 仅当终端只能使用原生 Unicode API 显示 Unicode 时才使用 |
打印操作总是带缓冲的 提供无缓冲的打印操作 |
P3107R5 | C++23 | 总是使用原生 Unicode API | 只在终端只能使用原生 Unicode API 显示 Unicode 时使用 |
P3235R3 | C++23 | 添加的函数名称 P3107R5 具有误导性 |
更改了函数名称 |
[编辑] 另请参阅
使用类型擦除参数表示打印到 stdout 或文件流 (函数) | |
(C++23) |
使用类型擦除的参数表示执行 Unicode 感知输出 (函数) |
(C++23) |
使用参数的格式化表示打印到 stdout 或文件流 (函数模板) |
(C++20) |
将参数的格式化表示存储在新字符串中 (函数模板) |
[编辑] 外部链接
1. | Unicode |
2. | The Unicode Standard Version 14.0 - Core Specification |