std:: make_format_args, std:: make_wformat_args
|
ヘッダーで定義
<format>
|
||
|
template
<
class
Context
=
std::
format_context
,
class
...
Args
>
/*format-arg-store*/
<
Context, Args...
>
|
(1) | (C++20以降) |
|
template
<
class
...
Args
>
/*format-arg-store*/
<
std::
wformat_context
, Args...
>
|
(2) | (C++20以降) |
書式化引数の配列を格納するオブジェクトを返し、暗黙的に std::basic_format_args<Context> に変換可能です。
typename
Context
::
template
formatter_type
<
std::
remove_const_t
<
Ti
>>
が
BasicFormatter
要件を満たさない場合、動作は未定義です(
Args
内の任意の
Ti
について)。
いずれかの型
Ti
が
Args
内に存在する場合、
Ti
が
__formattable_with
<
Context
>
を満たさないとき、プログラムは不適格です。
目次 |
パラメータ
| args... | - | 書式設定引数として使用される値 |
戻り値
書式設定引数を保持するオブジェクト。
各引数
t
の型が
T
であるとき、
TD
を
std::
remove_const_t
<
std::
remove_reference_t
<
T
>>
と定義する。結果における対応する
std::basic_format_arg
は以下のように決定される:
-
TDが bool またはContext::char_typeの場合、 std::basic_format_arg は t を格納する; -
そうでなければ、
TDが char であり、かつContext::char_typeが wchar_t である場合、 std::basic_format_arg は static_cast < wchar_t > ( static_cast < unsigned char > ( t ) ) を格納する; -
そうでなければ、
TDがサイズが int 以下の符号付き整数型である場合、 std::basic_format_arg は static_cast < int > ( t ) を格納する; -
そうでなければ、
TDがサイズが unsigned int を超えない符号なし整数型の場合、 std::basic_format_arg は static_cast < unsigned int > ( t ) を格納する; -
そうでなければ、
TDが long long 以下のサイズを持つ符号付き整数型の場合、 std::basic_format_arg は static_cast < long long > ( t ) を格納する; -
そうでなければ、
TDがサイズが unsigned long long を超えない符号なし整数型の場合、 std::basic_format_arg は static_cast < unsigned long long > ( t ) を格納する; -
そうでなければ、
TDが float 、 double または long double の場合、 std::basic_format_arg は t を格納する; -
そうでなければ、
TDが std::basic_string_view または std::basic_string の特殊化であり、かつTD::char_typeがContext::char_typeである場合、 std::basic_format_arg は std:: basic_string_view < Context :: char_type > ( t. data ( ) , t. size ( ) ) を格納する; - そうでなければ、 std:: decay_t < TD > が Context :: char_type * または const Context :: char_type * の場合、 std::basic_format_arg は static_cast < const Context :: char_type * > ( t ) を格納する;
- そうでなければ、 std:: is_void_v < std:: remove_pointer_t < TD >> が true または std:: is_null_pointer_v < TD > が true の場合、 std::basic_format_arg は static_cast < const void * > ( t ) を格納する;
-
そうでなければ、
std::basic_format_arg
は
tへの std:: basic_format_arg < Context > :: handle を格納し、handle::format()に必要な追加データも合わせて保持します。
注記
ユーザー定義型に対するフォーマット引数は参照セマンティクスを持ち、 args の寿命を延長しません。 args が戻り値よりも長く生存することを保証するのはプログラマの責任です。通常、結果はフォーマット関数への引数としてのみ使用されます。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_format_uchar
|
202311L
|
(C++20)
(DR) |
コードユニットの符号なし整数としてのフォーマット |
例
#include <array> #include <format> #include <iostream> #include <string_view> void raw_write_to_log(std::string_view users_fmt, std::format_args&& args) { static int n{}; std::clog << std::format("{:04} : ", n++) << std::vformat(users_fmt, args) << '\n'; } template<typename... Args> constexpr void log(Args&&... args) { // フォーマット文字列 "{} "... を生成 std::array<char, sizeof...(Args) * 3 + 1> braces{}; constexpr const char c[4] = "{} "; for (auto i{0uz}; i != braces.size() - 1; ++i) braces[i] = c[i % 3]; braces.back() = '\0'; raw_write_to_log(std::string_view{braces.data()}, std::make_format_args(args...)); } template<typename T> const T& unmove(T&& x) { return x; } int main() { log("Number", "of", "arguments", "is", "arbitrary."); log("Any type that meets the BasicFormatter requirements", "can be printed."); log("For example:", 1, 2.0, '3', "*42*"); raw_write_to_log("{:02} │ {} │ {} │ {}", std::make_format_args(unmove(1), unmove(2.0), unmove('3'), "4")); }
出力:
0000 : Number of arguments is arbitrary. 0001 : Any type that meets the BasicFormatter requirements can be printed. 0002 : For example: 1 2.0 3 *42* 0003 : 01 │ 2.0 │ 3 │ 4
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| P2418R2 | C++20 |
const使用可能でもコピー可能でもないオブジェクト
(ジェネレータ類似オブジェクトなど)はフォーマット不可 |
これらのオブジェクトのフォーマットを許可 |
| P2905R2 | C++20 |
make_format_args
が転送参照でrvalue引数を受け入れていた
|
左辺値参照のみを受け取る |
| P2909R4 | C++20 |
char
または
wchar_t
が範囲外の符号なし整数値として
フォーマットされる可能性があった |
コード単位はそのようなフォーマット前に
対応する符号なし型に変換される |
| LWG 3631 | C++20 | P2418R2適用後、cv修飾された引数が誤って処理されていた | 処理を修正 |
関連項目
|
(C++20)
(C++20)
(C++20)
|
すべてのフォーマット引数へのアクセスを提供するクラス
(クラステンプレート) |
|
(C++20)
|
型消去された引数表現を使用する
std::format
の非テンプレート版
(関数) |
|
(C++20)
|
型消去された引数表現を使用する
std::format_to
の非テンプレート版
(関数テンプレート) |