std::formatter
| ヘッダー <format> で定義 |
||
| template< class T, class CharT = char > struct formatter; |
(C++20以降) | |
std::formatter の有効化された特殊化は、指定された型のフォーマット規則を定義します。有効化された特殊化は BasicFormatter の要件を満たし、特に指定がない限り Formatter の要件も満たします。
std::formatter<T, CharT> の特殊化が有効化されていないすべての型 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) |
範囲 (range) のフォーマットサポート (クラステンプレート特殊化) |
| (C++23) |
std::stack のフォーマットサポート(class template specialization) |
| (C++23) |
std::queue のフォーマットサポート(class template specialization) |
std::priority_queue のフォーマットサポート(class template specialization) | |
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 の範囲としてフォーマット可能であった | 特殊化が無効化された |
[編集] 関連項目
| (C++20)(C++20)(C++20) |
すべてのフォーマット引数と出力イテレータを含むフォーマット状態 (クラステンプレート) |
| (C++23) |
型がフォーマット可能であること、すなわち std::formatter を特殊化し、parse および format メンバ関数を提供することを示します。(コンセプト) |
| (C++23) |
std::formatter の特殊化を範囲型に対して実装するのに役立つクラステンプレート。 (クラステンプレート) |