std::bad_any_cast
From cppreference.com
| ヘッダー <any> で定義 |
||
| class bad_any_cast : public std::bad_cast; |
(C++17以降) | |
std::any_cast の値返却形式が失敗した場合にスローされるオブジェクトの型を定義します。
目次 |
[編集] メンバ関数
| (コンストラクタ) |
新しいbad_any_castオブジェクトを構築します。(public member function) |
| operator= |
bad_any_castオブジェクトを置き換えます。(public member function) |
| what |
説明文字列を返す (public member function) |
std::bad_any_cast::bad_any_cast
| bad_any_cast() noexcept; |
(1) | (C++17以降) |
| bad_any_cast( const bad_any_cast& other ) noexcept; |
(2) | (C++17以降) |
what() 経由でアクセス可能な、実装定義のヌル終端バイト文字列を持つ新しいbad_any_castオブジェクトを構築します。
1) デフォルトコンストラクタ。
2) コピーコンストラクタ。 *this および other が両方とも動的型
std::bad_any_cast を持つ場合、代入後の std::strcmp(what(), other.what()) == 0 となります。パラメータ
| その他 | - | コピーする別の例外オブジェクト |
std::bad_any_cast::operator=
| bad_any_cast& operator=( const bad_any_cast& other ) noexcept; |
(C++17以降) | |
other の内容を代入します。 *this および other が両方とも動的型 std::bad_any_cast を持つ場合、代入後の std::strcmp(what(), other.what()) == 0 となります。
パラメータ
| その他 | - | 割り当てる別の例外オブジェクト |
戻り値
*this
std::bad_any_cast::what
virtual const char* what() const noexcept; |
(C++17以降) | |
説明文字列を返します。
戻り値
説明情報を含む、実装定義のヌル終端文字列へのポインタ。この文字列は std::wstring として変換および表示するのに適しています。このポインタは、それが取得された例外オブジェクトが破棄されるか、例外オブジェクトの非 const メンバー関数(例:コピー代入演算子)が呼び出されるまで、少なくとも有効であることが保証されます。
|
返された文字列は、定数評価中に通常のリテラルエンコーディングでエンコードされます。 |
(C++26以降) |
注釈
実装は what() をオーバーライドすることが許可されていますが、必須ではありません。
std::bad_cast から継承
std::exception から継承
メンバ関数
| [virtual] |
例外オブジェクトを破棄する ( std::exception の仮想 public メンバー関数) |
| [virtual] |
説明文字列を返す ( std::exception の仮想 public メンバー関数) |
[編集] 例
このコードを実行
#include <any> #include <cassert> #include <print> int main() { auto x = std::any(42); assert(std::any_cast<int>(x) == 42); // OK try { [[maybe_unused]] auto s = std::any_cast<std::string>(x); // throws } catch (const std::bad_any_cast& ex) { std::println("{}", ex.what()); } }
実行結果の例
bad any_cast