Namespaces
Variants

std:: format_to

From cppreference.net
ヘッダーで定義 <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... ) ) ; .


CharT char とするのはオーバーロード (1,3) の場合、 wchar_t とするのはオーバーロード (2,4) の場合です。

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

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

目次

パラメータ

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

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

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

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

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

(C++23以降)
(C++26以降)
  • その他のフォーマット可能な型の場合、フォーマット仕様はユーザー定義の formatter 特殊化によって決定される。
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)
引数のフォーマットされた表現を新しい文字列に格納する
(関数テンプレート)
出力イテレータを通じて引数のフォーマットされた表現を書き出し、指定されたサイズを超えない
(関数テンプレート)