名前空間
変種
操作

std::format_to

From cppreference.com
< cpp‎ | utility‎ | format
 
 
 
 
ヘッダー <format> で定義
template< class OutputIt, class... Args >

OutputIt format_to( OutputIt out,

                    std::format_string<Args...> fmt, Args&&... args );
(1) (C++20以降)
template< class OutputIt, class... Args >

OutputIt format_to( OutputIt out,

                    std::wformat_string<Args...> fmt, Args&&... args );
(2) (C++20以降)
template< class OutputIt, class... Args >

OutputIt format_to( OutputIt out, const std::locale& loc,

                    std::format_string<Args...> fmt, Args&&... args );
(3) (C++20以降)
template< class OutputIt, class... Args >

OutputIt format_to( OutputIt out, const std::locale& loc,

                    std::wformat_string<Args...> fmt, Args&&... args );
(4) (C++20以降)

フォーマット文字列 fmt に従って引数 args をフォーマットし、結果を出力イテレータ out に書き込みます。指定されている場合、 loc はロケール固有のフォーマットに使用されます。

以下と等価です。

1) return std::vformat_to(std::move(out), fmt.str, std::make_format_args(args...));
2) return std::vformat_to(std::move(out), fmt.str, std::make_wformat_args(args...));
3) return std::vformat_to(std::move(out), loc, fmt.str, std::make_format_args(args...));
4) return std::vformat_to(std::move(out), loc, fmt.str, std::make_wformat_args(args...));


オーバーロード (1,3) では CharTchar、オーバーロード (2,4) では wchar_t です。

これらのオーバーロードは、 OutputIt がコンセプト std::output_iterator<const CharT&> を満たす場合にのみ、オーバーロード解決に参加します。

次のいずれかの条件が満たされる場合、動作は未定義です。

目次

[edit] Parameters

out - 出力バッファへのイテレータ
fmt - フォーマット文字列を表すオブジェクト。フォーマット文字列は以下で構成される:
  • 通常の文字({} を除く)は、変更されずに出力にコピーされる。
  • エスケープシーケンス {{}} は、それぞれ出力で {} に置き換えられる。
  • 置換フィールド。

各置換フィールドは以下の形式を持つ。

{ arg-id (オプション) } (1)
{ arg-id (オプション) : format-spec } (2)
1) フォーマット指定なしの置換フィールド
2) フォーマット指定ありの置換フィールド
arg-id - フォーマットに使用する args 内の引数のインデックスを指定する。省略された場合、引数は順番に使用される。

フォーマット文字列内の arg-id は、すべて存在するか、すべて省略されなければならない。手動と自動のインデックス付けを混在させることはエラーである。

format-spec - 対応する引数に対する std::formatter 特殊化によって定義されるフォーマット指定。} で開始することはできない。

(C++23から)
(C++26以降)
  • その他のフォーマット可能な型の場合、フォーマット指定はユーザー定義の formatter 特殊化によって決定される。
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)
引数のフォーマット済み表現を新しい文字列に格納する
(関数テンプレート) [編集]
指定されたサイズを超えないように、引数のフォーマット済み表現を出力イテレータを介して書き出す
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)