名前空間
変種
操作

std::basic_format_string, std::format_string, std::wformat_string

From cppreference.com
< cpp‎ | utility‎ | format
 
 
 
 
ヘッダー <format> で定義
template< class CharT, class... Args >
struct basic_format_string;
(1) (C++20以降)
template< class... Args >

using format_string =

    basic_format_string<char, std::type_identity_t<Args>...>;
(2) (C++20以降)
template< class... Args >

using wformat_string =

    basic_format_string<wchar_t, std::type_identity_t<Args>...>;
(3) (C++20以降)

クラス テンプレート std::basic_format_string は、書式化関数によって使用される std::basic_string_view をラップします。

std::basic_format_string のコンストラクタは、コンパイル時に書式指定文字列のチェックを実行します。ただし、コンストラクタ引数が std::runtime_format によって返される場合は除きます(C++26 以降)

目次

[編集] メンバ関数

(コンストラクタ)
basic_format_string を構築し、引数が書式指定文字列でない場合はコンパイルエラーを発生させます。
(public member function)
get
ラップされた文字列を返します。
(public member function)

std::basic_format_string::basic_format_string

template< class T >
consteval basic_format_string( const T& s );
(1)
basic_format_string( /* runtime-format-string */<CharT> s ) noexcept;
(2) (C++26以降)
1) 文字列 s のビューを格納する basic_format_string オブジェクトを構築します。引数がコンパイル時定数でない場合、または書式化引数型 Args の書式指定文字列として解析できない場合、構築は不正です。
このオーバーロードは、const T&std::convertible_to<std::basic_string_view<CharT>> をモデル化する場合にのみ、オーバーロード解決に参加します。
2) std::runtime_format によって返される文字列 s のビューを格納する basic_format_string オブジェクトを構築します。構築時に書式指定文字列のチェックは実行されません。

パラメータ

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

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

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

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

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

(C++23から)
(C++26以降)
  • その他のフォーマット可能な型の場合、フォーマット指定はユーザー定義の formatter 特殊化によって決定される。

std::basic_format_string::get

constexpr std::basic_string_view<CharT> get() const noexcept;

格納されている文字列ビューを返します。

[編集] 備考

エイリアス テンプレート format_string および wformat_string は、テンプレート引数推論を抑制するために std::type_identity_t を使用します。通常、これらが関数パラメータとして出現する場合、そのテンプレート引数は他の関数引数から推論されます。

template<class... Args>
std::string format(std::format_string<Args...> fmt, Args&&... args);
 
auto s = format("{} {}", 1.0, 2);
// Calls format<double, int>. Args are deduced from 1.0, 2
// Due to the use of type_identity_t in format_string, template argument deduction
// does not consider the type of the format string.

[編集]

[編集] 欠陥報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
P2508R1 C++20 この機能にはユーザーから見える名前がない basic_format_stringという名前が公開された
English 日本語 中文(简体) 中文(繁體)