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 はロケール固有のフォーマットに使用されます。
次と同等:
CharT
を
char
とするのはオーバーロード
(1,3)
の場合、
wchar_t
とするのはオーバーロード
(2,4)
の場合です。
これらのオーバーロードは、
OutputIt
がコンセプト
std::
output_iterator
<
const
CharT
&
>
を満たす場合にのみ、オーバーロード解決に参加します。
以下のいずれかの条件が満たされる場合、動作は未定義です:
-
OutputItは std:: output_iterator < const CharT & > をモデル化しません。 -
std::
formatter
<
Ti, CharT
>
が一部の
Args内のTiについて BasicFormatter 要件( std::make_format_args および std::make_wformat_args で要求される)を満たしていません。
目次 |
パラメータ
| out | - | 出力バッファへのイテレータ | ||||||||||||||||||||||||||||||||||||||||||||||
| fmt | - |
各置換フィールドは以下の形式を持つ:
1)
フォーマット指定なしの置換フィールド
2)
フォーマット指定ありの置換フィールド
|
||||||||||||||||||||||||||||||||||||||||||||||
| args... | - | フォーマットする引数 | ||||||||||||||||||||||||||||||||||||||||||||||
| loc | - | std::locale ロケール固有のフォーマットに使用されるロケール | ||||||||||||||||||||||||||||||||||||||||||||||
戻り値
出力範囲の終端を超えたイテレータ。
例外
formatterまたはiterator操作によってスローされた例外を伝播します。
注記
書式文字列が定数式でない場合はエラーです
、
std::runtime_format
の結果から初期化されている場合を除きます
(C++26以降)
。
std::vformat_to
にはこの要件はありません。
例
#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()!
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 修正後の動作 |
|---|---|---|---|
| LWG 3539 | C++20 | out move-only iterator では使用できなかった | 使用可能になった |
| P2216R3 | C++20 | 不正なフォーマット文字列に対して std::format_error を送出 | 代わりにコンパイル時エラーとなる |
| P2418R2 | C++20 |
const-usable でも copyable でもないオブジェクト
(ジェネレータのようなオブジェクト)はフォーマット不可能 |
これらのオブジェクトのフォーマットを許可 |
| P2508R1 | C++20 | この機能に対するユーザー可視の名前が存在しない |
名前
basic_format_string
が公開される
|
関連項目
|
(C++20)
|
引数のフォーマットされた表現を新しい文字列に格納する
(関数テンプレート) |
|
(C++20)
|
出力イテレータを通じて引数のフォーマットされた表現を書き出し、指定されたサイズを超えない
(関数テンプレート) |