std::format_to
| ヘッダー <format> で定義 |
||
| template< class OutputIt, class... Args > OutputIt format_to( OutputIt out, |
(1) | (C++20以降) |
| template< class OutputIt, class... Args > OutputIt format_to( OutputIt out, |
(2) | (C++20以降) |
| template< class OutputIt, class... Args > OutputIt format_to( OutputIt out, const std::locale& loc, |
(3) | (C++20以降) |
| template< class OutputIt, class... Args > OutputIt format_to( OutputIt out, const std::locale& loc, |
(4) | (C++20以降) |
フォーマット文字列 fmt に従って引数 args をフォーマットし、結果を出力イテレータ out に書き込みます。指定されている場合、 loc はロケール固有のフォーマットに使用されます。
以下と等価です。
オーバーロード (1,3) では CharT は char、オーバーロード (2,4) では wchar_t です。
これらのオーバーロードは、 OutputIt がコンセプト std::output_iterator<const CharT&> を満たす場合にのみ、オーバーロード解決に参加します。
次のいずれかの条件が満たされる場合、動作は未定義です。
-
OutputItは std::output_iterator<const CharT&> をモデル化しません。 -
Args内のいくつかのTiについて、 std::formatter<Ti, CharT> は BasicFormatter の要件を満たさないため、 std::make_format_args および std::make_wformat_args から要求されます。
目次 |
[edit] Parameters
| out | - | 出力バッファへのイテレータ | ||||||||||||||||||||||||||||||||||||||||||||||
| fmt | - |
各置換フィールドは以下の形式を持つ。
1) フォーマット指定なしの置換フィールド
2) フォーマット指定ありの置換フィールド
| ||||||||||||||||||||||||||||||||||||||||||||||
| args... | - | フォーマットする引数 | ||||||||||||||||||||||||||||||||||||||||||||||
| loc | - | ロケール固有のフォーマットに使用される std::locale | ||||||||||||||||||||||||||||||||||||||||||||||
[edit] Return value
出力範囲の終端以降のイテレータ。
[edit] Exceptions
フォーマッタまたはイテレータ操作によってスローされた例外を伝播します。
[edit] Notes
P2216R3 によると、フォーマット文字列が定数式でない場合はエラーとなります。この場合、 std::vformat_to (または std::runtime_format ( C++26 以降))を使用できます。
[edit] Example
#include <format> #include <iostream> #include <iterator> #include <string> int main() { std::string buffer; std::format_to ( std::back_inserter(buffer), // < OutputIt "Hello, C++{}!\n", // < fmt "20" // < arg ); std::cout << buffer; buffer.clear(); std::format_to ( std::back_inserter(buffer), // < OutputIt "Hello, {0}::{1}!{2}", // < fmt "std", // < arg {0} "format_to()", // < arg {1} "\n", // < arg {2} "extra param(s)..." // < unused ); std::cout << buffer << std::flush; std::wstring wbuffer; std::format_to ( std::back_inserter(wbuffer),// < OutputIt L"Hello, {2}::{1}!{0}", // < fmt L"\n", // < arg {0} L"format_to()", // < arg {1} L"std", // < arg {2} L"...is not..." // < unused L"...an error!" // < unused ); std::wcout << wbuffer; }
出力
Hello, C++20! Hello, std::format_to()! Hello, std::format_to()!
[edit] Defect reports
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 3539 | C++20 | out はムーブオンリーイテレータではありえなかった | そうである場合がある |
| P2216R3 | C++20 | 無効なフォーマット文字列に対して std::format_error をスローします。 | 代わりにコンパイル時エラーとなる |
| P2418R2 | C++20 | const-usableでもコピー可能でもないオブジェクト (ジェネレータのようなオブジェクトなど)はフォーマットできない |
これらのオブジェクトのフォーマットを許可する |
| P2508R1 | C++20 | この機能にはユーザーから見える名前がない | basic_format_stringという名前が公開された |
[edit] See also
| (C++20) |
引数のフォーマット済み表現を新しい文字列に格納する (関数テンプレート) |
| (C++20) |
指定されたサイズを超えないように、引数のフォーマット済み表現を出力イテレータを介して書き出す (関数テンプレート) |