std::format_error
From cppreference.com
| ヘッダー <format> で定義 |
||
| class format_error; |
(C++20以降) | |
フォーマットライブラリのエラーを報告するためにスローされる例外オブジェクトの型を定義します。
継承図
目次 |
[編集] メンバー関数
| (コンストラクタ) |
指定されたメッセージを持つ新しいformat_errorオブジェクトを構築します。(public member function) |
| operator= |
format_errorオブジェクトを置き換えます。(public member function) |
std::format_error::format_error
| format_error( const std::string& what_arg ); |
(1) | |
| format_error( const char* what_arg ); |
(2) | |
| format_error( const format_error& other ) noexcept; |
(3) | |
1) 説明文字列として
what_argを使用して例外オブジェクトを構築します。構築後、strcmp(what(), what_arg.c_str()) == 0となります。2) 説明文字列として
what_argを使用して例外オブジェクトを構築します。構築後、strcmp(what(), what_arg) == 0となります。3) コピーコンストラクタ。
*thisとotherが両方とも動的型std::format_errorを持つ場合、代入後strcmp(what(), other.what()) == 0となります。コピーコンストラクタから例外をスローすることはできません。パラメータ
| what_arg | - | 説明文字列 |
| その他 | - | コピーする別の例外オブジェクト |
例外
1,2) std::bad_alloc をスローする可能性があります。
注釈
std::format_errorのコピーは例外をスローすることが許可されていないため、このメッセージは通常、別途割り当てられた参照カウント文字列として内部に格納されます。これも、std::string&&を受け取るコンストラクタがない理由です。いずれにせよ内容をコピーする必要があるためです。
派生した標準例外クラスには、公開アクセス可能なコピーコンストラクタが必要です。what()によって取得される説明文字列が元のオブジェクトとコピーされたオブジェクトで同じである限り、暗黙的に定義できます。
std::format_error::operator=
| format_error& operator=( const format_error& other ) noexcept; |
||
otherの内容で内容を代入します。*thisとotherが両方とも動的型std::format_errorを持つ場合、代入後strcmp(what(), other.what()) == 0となります。コピー代入演算子から例外をスローすることはできません。
パラメータ
| その他 | - | 割り当てる別の例外オブジェクト |
戻り値
*this
注釈
派生した標準例外クラスには、公開アクセス可能なコピー代入演算子が必要です。what()によって取得される説明文字列が元のオブジェクトとコピーされたオブジェクトで同じである限り、暗黙的に定義できます。
std::runtime_error から継承
std::exception から継承
メンバ関数
| [virtual] |
例外オブジェクトを破棄する ( std::exception の仮想 public メンバー関数) |
| [virtual] |
説明文字列を返す ( std::exception の仮想 public メンバー関数) |
[編集] 例
このコードを実行
#include <format> #include <print> #include <string_view> #include <utility> int main() { try { auto x13{37}; auto args{std::make_format_args(x13)}; std::ignore = std::vformat("{:()}", args); // throws } catch(const std::format_error& ex) { std::println("{}", ex.what()); } }
実行結果の例
format error: failed to parse format-spec