名稱空間
變體
操作

std::format_to_n, std::format_to_n_result

來自 cppreference.com
 
 
 
 
定義於標頭檔案 <format>
template< class OutputIt, class... Args >

std::format_to_n_result<OutputIt>
    format_to_n( OutputIt out, std::iter_difference_t<OutputIt> n,

                 std::format_string<Args...> fmt, Args&&... args );
(1) (C++20 起)
template< class OutputIt, class... Args >

std::format_to_n_result<OutputIt>
    format_to_n( OutputIt out, std::iter_difference_t<OutputIt> n,

                 std::wformat_string<Args...> fmt, Args&&... args );
(2) (C++20 起)
template< class OutputIt, class... Args >

std::format_to_n_result<OutputIt>
    format_to_n( OutputIt out, std::iter_difference_t<OutputIt> n,
                 const std::locale& loc,

                 std::format_string<Args...> fmt, Args&&... args );
(3) (C++20 起)
template< class OutputIt, class... Args >

std::format_to_n_result<OutputIt>
    format_to_n( OutputIt out, std::iter_difference_t<OutputIt> n,
                 const std::locale& loc,

                 std::wformat_string<Args...> fmt, Args&&... args );
(4) (C++20 起)
輔助型別
template< class OutputIt >

struct format_to_n_result {
    OutputIt out;
    std::iter_difference_t<OutputIt> size;

};
(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 中的任何 Tistd::formatter<std::remove_cvref_t<Ti>, CharT> 不滿足 BasicFormatter 要求,則行為未定義。

5) std::format_to_n_result 沒有基類,也沒有除 outsize 和隱式宣告的特殊成員函式之外的成員。

目錄

[編輯] 引數

out - 指向輸出緩衝區的迭代器
n - 寫入緩衝區的最大字元數
fmt - 表示格式化字串的物件。格式化字串由以下部分組成:
  • 普通字元(除了 {}),它們被原樣複製到輸出,
  • 轉義序列 {{}},它們在輸出中分別被替換為 {},以及
  • 替換欄位。

每個替換欄位具有以下格式:

{ arg-id (可選) } (1)
{ arg-id (可選) : format-spec } (2)
1) 沒有格式化規範的替換欄位
2) 帶有格式化規範的替換欄位
arg-id - 指定 args 中用於格式化的引數的索引;如果省略,則按順序使用引數。

格式化字串中的 arg-id 必須全部存在或全部省略。混合手動和自動索引是錯誤的。

format-spec - 由相應引數的 std::formatter 特化定義的格式規範。不能以 } 開頭。

(C++23 起)
(C++26 起)
  • 對於其他可格式化型別,格式化規範由使用者定義的 formatter 特化決定。
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)
透過輸出迭代器寫出其引數的格式化表示
(函式模板) [編輯]
確定儲存其引數格式化表示所需的字元數
(函式模板) [編輯]