std::vprint_unicode, std::vprint_unicode_buffered
來自 cppreference.com
定義於標頭檔案 <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 |