命名空间
变体
操作

std::formatter

来自 cppreference.cn
< cpp‎ | utility‎ | format
 
 
 
 
定义于头文件 <format>
template< class T, class CharT = char >
struct formatter;
(自 C++20 起)

std::formatter 的已启用特化为给定类型定义了格式化规则。已启用特化满足 BasicFormatter 要求,并且,除非另有说明,否则也满足 Formatter 要求。

对于所有类型 TCharT,如果未启用特化 std::formatter<T, CharT>,则该特化是完整类型并且被禁用。

禁用的特化不满足 Formatter 要求,并且以下所有项均为 false

内容

[编辑] 基本标准特化

在以下列表中,CharTcharwchar_tArithmeticT 是任何 cv-非限定算术类型,但 charwchar_tchar8_tchar16_tchar32_t 除外

字符格式化器
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 的格式化支持
(类模板特化) [编辑]
ranges 的格式化支持
(类模板特化) [编辑]
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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 3944 C++23 一些 char 序列可格式化为 wchar_t 的范围 禁用添加的特化

[编辑] 参见

格式化状态,包括所有格式化参数和输出迭代器
(类模板) [编辑]
指定类型是可格式化的,即,它特化 std::formatter 并提供成员函数 parseformat
(概念) [编辑]
帮助为范围类型实现 std::formatter 特化的类模板
(类模板) [编辑]