std::formatter
來自 cppreference.com
定義於標頭檔案 <format> |
||
template< class T, class CharT = char > struct formatter; |
(C++20 起) | |
std::formatter
的已啟用特化定義了給定型別的格式化規則。已啟用的特化滿足 BasicFormatter 要求,並且,除非另有說明,也滿足 Formatter 要求。
對於所有型別 T
和 CharT
,如果未啟用 std::formatter<T, CharT>
的特化,則該特化是完整型別且被停用。
停用的特化不滿足 Formatter 要求,並且以下均為 false
- std::is_default_constructible_v
- std::is_copy_constructible_v
- std::is_move_constructible_v
- std::is_copy_assignable_v
- std::is_move_assignable_v.
目錄 |
[編輯] 基本標準特化
在以下列表中,CharT
是 char 或 wchar_t,ArithmeticT
是除了 char、wchar_t、char8_t、char16_t 或 char32_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 範圍
啟用除錯的格式化器特化額外提供一個公共非靜態成員函式 constexpr void set_debug_format();,它修改格式化器物件的狀態,使其將值格式化為 轉義並加引號,就像上次呼叫 每個字串或字元型別的格式化器特化都啟用除錯。 |
(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 的格式化支援(類模板特化) | |
對 pair 和 tuple 的格式化支援(類模板特化) | |
(C++23) |
對範圍的格式化支援 (類模板特化) |
(C++23) |
對 std::stack 的格式化支援(類模板特化) |
(C++23) |
對 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 範圍 | 已新增停用特化 |
[編輯] 另請參閱
(C++20 起)(C++20 起)(C++20 起) |
格式化狀態,包括所有格式化引數和輸出迭代器 (類模板) |
(C++23) |
指定型別是可格式化的,即它特化了 std::formatter 並提供了成員函式 parse 和 format (概念) |
(C++23) |
類模板,有助於實現範圍型別的 std::formatter 特化 (類模板) |