std::format_to_n, std::format_to_n_result
定義於標頭檔案 <format> |
||
template< class OutputIt, class... Args > std::format_to_n_result<OutputIt> |
(1) | (C++20 起) |
template< class OutputIt, class... Args > std::format_to_n_result<OutputIt> |
(2) | (C++20 起) |
template< class OutputIt, class... Args > std::format_to_n_result<OutputIt> |
(3) | (C++20 起) |
template< class OutputIt, class... Args > std::format_to_n_result<OutputIt> |
(4) | (C++20 起) |
輔助型別 |
||
template< class OutputIt > struct format_to_n_result { |
(5) | (C++20 起) |
根據格式字串 fmt 格式化 args,並將結果寫入輸出迭代器 out。最多寫入 n 個字元。如果存在,loc 用於語言環境特定的格式化。
令 CharT
為過載 (1,3) 的 char,過載 (2,4) 的 wchar_t。
這些過載僅在 OutputIt
滿足概念 std::output_iterator<const CharT&> 時參與過載決議。
如果 OutputIt
不滿足 (符合語義要求) 概念 std::output_iterator<const CharT&>,或者對於 Args
中的任何 Ti
,std::formatter<std::remove_cvref_t<Ti>, CharT> 不滿足 BasicFormatter 要求,則行為未定義。
std::format_to_n_result
沒有基類,也沒有除 out、size
和隱式宣告的特殊成員函式之外的成員。目錄 |
[編輯] 引數
out | - | 指向輸出緩衝區的迭代器 | ||||||||||||||||||||||||||||||||||||||||||||||
n | - | 寫入緩衝區的最大字元數 | ||||||||||||||||||||||||||||||||||||||||||||||
fmt | - |
每個替換欄位具有以下格式:
1) 沒有格式化規範的替換欄位
2) 帶有格式化規範的替換欄位
| ||||||||||||||||||||||||||||||||||||||||||||||
args... | - | 要格式化的引數 | ||||||||||||||||||||||||||||||||||||||||||||||
loc | - | 用於特定於語言環境的格式化的 std::locale |
[編輯] 返回值
一個 format_to_n_result
,其中 out 成員是輸出範圍末尾之後的迭代器,size
成員是總的(未截斷的)輸出大小。
[編輯] 異常
傳播 formatter 或迭代器操作丟擲的任何異常。
[編輯] 注意
在 GCC-13.3 之前的 libstdc++ 實現中,報告正確的 format_to_n_result::out 值時存在 一個錯誤。
[編輯] 示例
在 Godbolt 的編譯器瀏覽器上: clang (trunk) + libc++, GCC (trunk) + libstdc++。
#include <format> #include <initializer_list> #include <iomanip> #include <iostream> #include <string_view> int main() { char buffer[64]; for (std::size_t max_chars_to_write : {std::size(buffer) - 1, 23uz, 21uz}) { const std::format_to_n_result result = std::format_to_n( buffer, max_chars_to_write, "Hubble's H{2} {3} {0}{4}{1} km/sec/Mpc.", // 24 bytes w/o formatters 71, // {0}, occupies 2 bytes 8, // {1}, occupies 1 byte "\u2080", // {2}, occupies 3 bytes, '₀' (SUBSCRIPT ZERO) "\u2245", // {3}, occupies 3 bytes, '≅' (APPROXIMATELY EQUAL TO) "\u00B1" // {4}, occupies 2 bytes, '±' (PLUS-MINUS SIGN) ); // 24 + 2 + 1 + 3 + 3 + 2 == 35, no trailing '\0' *result.out = '\0'; // adds terminator to buffer const std::string_view str(buffer, result.out); std::cout << "Buffer until '\\0': " << std::quoted(str) << '\n' << "Max chars to write: " << max_chars_to_write << '\n' << "result.out offset: " << result.out - buffer << '\n' << "Untruncated output size: " << result.size << "\n\n"; } }
輸出
Buffer until '\0': "Hubble's H₀ ≅ 71±8 km/sec/Mpc." Max chars to write: 63 result.out offset: 35 Untruncated output size: 35 Buffer until '\0': "Hubble's H₀ ≅ 71±8" Max chars to write: 23 result.out offset: 23 Untruncated output size: 35 Buffer until '\0': "Hubble's H₀ ≅ 71�" Max chars to write: 21 result.out offset: 21 Untruncated output size: 35
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
P2216R3 | C++20 | 對無效格式字串丟擲 std::format_error | 無效格式字串導致編譯時錯誤 |
P2418R2 | C++20 | 既不可 const 使用也不可複製的物件 (例如生成器類物件) 不可格式化 |
允許格式化這些物件 |
P2508R1 | C++20 | 該功能沒有使用者可見的名稱 | 公開了名稱 basic_format_string |
[編輯] 參閱
(C++20) |
將引數的格式化表示儲存在新字串中 (函式模板) |
(C++20) |
透過輸出迭代器寫出其引數的格式化表示 (函式模板) |
(C++20) |
確定儲存其引數格式化表示所需的字元數 (函式模板) |