名稱空間
變體
操作

std::formatter

來自 cppreference.com
< cpp‎ | 工具‎ | 格式化
 
 
 
 
定義於標頭檔案 <format>
template< class T, class CharT = char >
struct formatter;
(C++20 起)

std::formatter 的已啟用特化定義了給定型別的格式化規則。已啟用的特化滿足 BasicFormatter 要求,並且,除非另有說明,也滿足 Formatter 要求。

對於所有型別 TCharT,如果未啟用 std::formatter<T, CharT> 的特化,則該特化是完整型別且被停用。

停用的特化不滿足 Formatter 要求,並且以下均為 false

目錄

[編輯] 基本標準特化

在以下列表中,CharTcharwchar_tArithmeticT 是除了 charwchar_tchar8_tchar16_tchar32_t 之外的任何 cv-非限定算術型別。

字元格式化器
template<>
struct formatter<char, char>;
(1)
template<>
struct formatter<char, wchar_t>;
(2)
template<>
struct formatter<wchar_t, wchar_t>;
(3)
字串格式化器
template<>
struct formatter<CharT*, CharT>;
(4)
template<>
struct formatter<const CharT*, CharT>;
(5)
template< std::size_t N >
struct formatter<CharT[N], CharT>;
(6)
template< class Traits, class Alloc >
struct formatter<std::basic_string<CharT, Traits, Alloc>, CharT>;
(7)
template< class Traits >
struct formatter<std::basic_string_view<CharT, Traits>, CharT>;
(8)
算術格式化器
template<>
struct formatter<ArithmeticT, CharT>;
(9)
指標格式化器
template<>
struct formatter<std::nullptr_t, CharT>;
(10)
template<>
struct formatter<void*, CharT>;
(11)
template<>
struct formatter<const void*, CharT>;
(12)

其他指標和成員指標的格式化器被停用。

需要編碼轉換的特化,例如 std::formatter<wchar_t, char>std::formatter<const char*, wchar_t> 被停用。

以下特化在 C++23 中仍被停用,以避免將某些 char 序列格式化為 wchar_t 範圍

停用的 wchar_t 格式化器
template<>
struct formatter<char*, wchar_t>;
(1)
template<>
struct formatter<const char*, wchar_t>;
(2)
template< std::size_t N >
struct formatter<char[N], wchar_t>;
(3)
template< class Traits, class Allocator >
struct formatter<std::basic_string<char, Traits, Allocator>, wchar_t>;
(4)
template< class Traits >
struct formatter<std::basic_string_view<char, Traits>, wchar_t>;
(5)

啟用除錯的格式化器特化額外提供一個公共非靜態成員函式 constexpr void set_debug_format();,它修改格式化器物件的狀態,使其將值格式化為 轉義並加引號,就像上次呼叫 parse 解析的格式說明符的 type? 一樣。

每個字串或字元型別的格式化器特化都啟用除錯

(C++23 起)

[編輯] 標準格式規範

[編輯] 庫型別的標準特化

duration 的格式化支援
(類模板特化) [編輯]
sys_time 的格式化支援
(類模板特化) [編輯]
utc_time 的格式化支援
(類模板特化) [編輯]
tai_time 的格式化支援
(類模板特化) [編輯]
gps_time 的格式化支援
(類模板特化) [編輯]
file_time 的格式化支援
(類模板特化) [編輯]
local_time 的格式化支援
(類模板特化) [編輯]
day 的格式化支援
(類模板特化) [編輯]
month 的格式化支援
(類模板特化) [編輯]
year 的格式化支援
(類模板特化) [編輯]
weekday 的格式化支援
(類模板特化) [編輯]
weekday_indexed 的格式化支援
(類模板特化) [編輯]
weekday_last 的格式化支援
(類模板特化) [編輯]
month_day 的格式化支援
(類模板特化) [編輯]
month_day_last 的格式化支援
(類模板特化) [編輯]
month_weekday 的格式化支援
(類模板特化) [編輯]
month_weekday_last 的格式化支援
(類模板特化) [編輯]
year_month 的格式化支援
(類模板特化) [編輯]
year_month_day 的格式化支援
(類模板特化) [編輯]
year_month_day_last 的格式化支援
(類模板特化) [編輯]
year_month_weekday 的格式化支援
(類模板特化) [編輯]
year_month_weekday_last 的格式化支援
(類模板特化) [編輯]
hh_mm_ss 的格式化支援
(類模板特化) [編輯]
sys_info 的格式化支援
(類模板特化) [編輯]
local_info 的格式化支援
(類模板特化) [編輯]
zoned_time 的格式化支援
(類模板特化) [編輯]
basic_stacktrace 的格式化支援
(類模板特化) [編輯]
stacktrace_entry 的格式化支援
(類模板特化) [編輯]
thread::id 的格式化支援
(類模板特化) [編輯]
vector<bool>::reference 的格式化支援
(類模板特化) [編輯]
pairtuple 的格式化支援
(類模板特化) [編輯]
對範圍的格式化支援
(類模板特化) [編輯]
std::stack 的格式化支援
(類模板特化) [編輯]
std::queue 的格式化支援
(類模板特化) [編輯]
std::priority_queue 的格式化支援
(類模板特化) [編輯]
filesystem::path 的格式化支援
(類模板特化) [編輯]

[編輯] 示例

#include <algorithm>
#include <format>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string_view>
 
struct QuotableString : std::string_view
{};
 
template<>
struct std::formatter<QuotableString, char>
{
    bool quoted = false;
 
    template<class ParseContext>
    constexpr ParseContext::iterator parse(ParseContext& ctx)
    {
        auto it = ctx.begin();
        if (it == ctx.end())
            return it;
 
        if (*it == '#')
        {
            quoted = true;
            ++it;
        }
        if (it != ctx.end() && *it != '}')
            throw std::format_error("Invalid format args for QuotableString.");
 
        return it;
    }
 
    template<class FmtContext>
    FmtContext::iterator format(QuotableString s, FmtContext& ctx) const
    {
        std::ostringstream out;
        if (quoted)
            out << std::quoted(s);
        else
            out << s;
 
        return std::ranges::copy(std::move(out).str(), ctx.out()).out;
    }
};
 
int main()
{
    QuotableString a("be"), a2(R"( " be " )");
    QuotableString b("a question");
    std::cout << std::format("To {0} or not to {0}, that is {1}.\n", a, b);
    std::cout << std::format("To {0:} or not to {0:}, that is {1:}.\n", a, b);
    std::cout << std::format("To {0:#} or not to {0:#}, that is {1:#}.\n", a2, b);
}

輸出

To be or not to be, that is a question.
To be or not to be, that is a question.
To " \" be \" " or not to " \" be \" ", that is "a question".

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 3944 C++23 某些 char 序列可被格式化為 wchar_t 範圍 已新增停用特化

[編輯] 另請參閱

格式化狀態,包括所有格式化引數和輸出迭代器
(類模板) [編輯]
指定型別是可格式化的,即它特化了 std::formatter 並提供了成員函式 parseformat
(概念) [編輯]
類模板,有助於實現範圍型別的 std::formatter 特化
(類模板) [編輯]