std:: formatter
|
定義済みヘッダー
<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 > などの特殊化は無効化されています。
|
以下の特殊化は、一部の char シーケンスを wchar_t の範囲としてフォーマットすることを避けるため、C++23においても無効化されたままです:
デバッグ有効
フォーマッタ特殊化は、さらに公開非静的メンバ関数
constexpr
void
set_debug_format
(
)
;
を提供し、フォーマッタオブジェクトの状態を変更して、最後の
文字列型または文字型に対する各フォーマッタ特殊化は デバッグ有効 です。 |
(C++23以降) |
標準フォーマット仕様
|
このセクションは不完全です
理由: 標準フォーマット仕様は別の ページ に移動されました。このセクションのタイトルは、このセクションへのリンクのために一時的に保持されています。すべてのリンクが解決された後、このセクションは削除されます。 |
ライブラリ型の標準特殊化
duration
のフォーマットサポート
(クラステンプレートの特殊化) |
|
sys_timeのフォーマットサポート
sys_time
(クラステンプレートの特殊化) |
|
utc_timeのフォーマットサポート
utc_time
(クラステンプレートの特殊化) |
|
tai_timeのフォーマットサポート
tai_time
(クラステンプレートの特殊化) |
|
gps_time
のフォーマットサポート
(クラステンプレートの特殊化) |
|
file_timeのフォーマットサポート
file_time
(クラステンプレートの特殊化) |
|
local_time
のフォーマットサポート
(クラステンプレートの特殊化) |
|
day
のフォーマットサポート
(クラステンプレート特殊化) |
|
monthのフォーマットサポート
month
(クラステンプレートの特殊化) |
|
year
のフォーマットサポート
(クラステンプレートの特殊化) |
|
weekdayのフォーマットサポート
weekday
(クラステンプレートの特殊化) |
|
weekday_indexedのフォーマットサポート
weekday_indexed
(クラステンプレートの特殊化) |
|
weekday_lastのフォーマットサポート
weekday_last
(クラステンプレートの特殊化) |
|
month_dayのフォーマットサポート
month_day
(クラステンプレートの特殊化) |
|
month_day_lastのフォーマットサポート
month_day_last
(クラステンプレートの特殊化) |
|
month_weekday
の書式設定サポート
(クラステンプレートの特殊化) |
|
month_weekday_lastのフォーマットサポート
month_weekday_last
(クラステンプレートの特殊化) |
|
year_month
のフォーマットサポート
(クラステンプレートの特殊化) |
|
year_month_dayのフォーマットサポート
year_month_day
(クラステンプレートの特殊化) |
|
year_month_day_last
のフォーマットサポート
(クラステンプレートの特殊化) |
|
year_month_weekdayのフォーマットサポート
year_month_weekday
(クラステンプレートの特殊化) |
|
year_month_weekday_lastのフォーマットサポート
year_month_weekday_last
(クラステンプレートの特殊化) |
|
hh_mm_ss
のフォーマットサポート
(クラステンプレートの特殊化) |
|
sys_infoのフォーマットサポート
sys_info
(クラステンプレートの特殊化) |
|
local_infoのフォーマットサポート
local_info
(クラステンプレートの特殊化) |
|
zoned_time
のフォーマットサポート
(クラステンプレートの特殊化) |
|
basic_stacktrace
のフォーマット対応
(クラステンプレートの特殊化) |
|
stacktrace_entryのフォーマットサポート
stacktrace_entry
(クラステンプレートの特殊化) |
|
|
(C++23)
|
thread::id
のフォーマットサポート
(クラステンプレートの特殊化) |
vector<bool>::reference
のフォーマット対応
(クラステンプレートの特殊化) |
|
|
(C++23)
|
pair
および
tuple
の
フォーマット対応 (クラステンプレートの特殊化) |
|
(C++23)
|
範囲のフォーマットサポート
(クラステンプレートの特殊化) |
|
(C++23)
|
std::stackのフォーマットサポート
std::stack
(クラステンプレートの特殊化) |
|
(C++23)
|
std::queueのフォーマットサポート
std::queue
(クラステンプレートの特殊化) |
std::priority_queueの
std::priority_queue
フォーマット対応
(クラステンプレートの特殊化) |
|
filesystem::pathのフォーマットサポート
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
特殊化の実装を支援するクラステンプレート
(クラステンプレート) |