std:: format
|
ヘッダーで定義
<format>
|
||
|
template
<
class
...
Args
>
std:: string format ( std:: format_string < Args... > fmt, Args && ... args ) ; |
(1) | (C++20以降) |
|
template
<
class
...
Args
>
std:: wstring format ( std:: wformat_string < Args... > fmt, Args && ... args ) ; |
(2) | (C++20以降) |
|
template
<
class
...
Args
>
std::
string
format
(
const
std::
locale
&
loc,
|
(3) | (C++20以降) |
|
template
<
class
...
Args
>
std::
wstring
format
(
const
std::
locale
&
loc,
|
(4) | (C++20以降) |
書式文字列 fmt に従って args をフォーマットし、結果を文字列として返します。存在する場合、 loc はロケール固有のフォーマットに使用されます。
書式文字列
fmt
はコンパイル時にチェックされます
(
std::runtime_format
の結果から初期化された場合を除く)
(C++26以降)
。コンパイル時に、書式文字列がフォーマット対象の引数の型に対して無効であることが判明した場合、コンパイルエラーが発生します。
以下の要件は、
Args
内の各型
T
に適用されます。ここで
CharT
は、オーバーロード
(1,3)
では
char
、オーバーロード
(2,4)
では
wchar_t
です:
- std:: formatter < T, CharT > は BasicFormatter を満たさなければならない
- std:: formatter < T, CharT > :: parse ( ) はコンパイル時フォーマット文字列チェックの目的で constexpr でなければならない
目次 |
パラメータ
| fmt | - |
各置換フィールドは以下の形式を持つ:
1)
フォーマット指定なしの置換フィールド
2)
フォーマット指定ありの置換フィールド
|
||||||||||||||||||||||||||||||||||||||||||||||
| args... | - | フォーマットされる引数 | ||||||||||||||||||||||||||||||||||||||||||||||
| loc | - | ロケール固有のフォーマットに使用される std::locale | ||||||||||||||||||||||||||||||||||||||||||||||
戻り値
フォーマットされた結果を保持する文字列オブジェクト。
例外
確保に失敗した場合は std::bad_alloc をスローします。また、任意のフォーマッタによってスローされた例外を伝播します。
注記
フォーマット文字列が必要とする以上の引数を提供してもエラーにはなりません:
std::format("{} {}!", "Hello", "world", "something"); // OK、「Hello world!」を生成
書式文字列が定数式でない場合はエラーです
、
std::runtime_format
の結果から初期化されている場合を除く
(C++26以降)
。
std::vformat
にはこの要件はありません。
std::string f1(std::string_view runtime_format_string) { // return std::format(runtime_format_string, "x", 42); // エラー char v1[] = "x"; int v2 = 42; return std::vformat(runtime_format_string, std::make_format_args(v1, v2)); // OK } std::string f2(std::string_view runtime_format_string) { return std::format(std::runtime_format(runtime_format_string), "x", 42); // OK (C++26) }
例
#include <format> #include <iostream> #include <set> #include <string> #include <string_view> template<typename... Args> std::string dyna_print(std::string_view rt_fmt_str, Args&&... args) { return std::vformat(rt_fmt_str, std::make_format_args(args...)); } int main() { #ifdef __cpp_lib_format_ranges const std::set<std::string_view> continents { "Africa", "America", "Antarctica", "Asia", "Australia", "Europe" }; std::cout << std::format("Hello {}!\n", continents); #else std::cout << std::format("Hello {}!\n", "continents"); #endif std::string fmt; for (int i{}; i != 3; ++i) { fmt += "{} "; // フォーマット文字列を構築 std::cout << fmt << " : "; std::cout << dyna_print(fmt, "alpha", 'Z', 3.14, "unused"); std::cout << '\n'; } }
出力例:
Hello {"Africa", "America", "Antarctica", "Asia", "Australia", "Europe"}!
{} : alpha
{} {} : alpha Z
{} {} {} : alpha Z 3.14
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| P2216R3 | C++20 | 不正なフォーマット文字列に対して std::format_error をスロー | 不正なフォーマット文字列はコンパイル時エラーとなる |
| P2418R2 | C++20 |
const-usableでもコピー可能でもないオブジェクト
(ジェネレーターのようなオブジェクト)はフォーマット不可能 |
これらのオブジェクトのフォーマットを許可 |
| P2508R1 | C++20 | この機能に対するユーザー可視の名前が存在しない |
名前
basic_format_string
が公開される
|
関連項目
|
(C++20)
|
引数の書式化された表現を出力イテレータを通して書き出す
(関数テンプレート) |
|
(C++20)
|
引数の書式化された表現を出力イテレータを通して書き出す(指定されたサイズを超えない)
(関数テンプレート) |