std:: bad_cast
|
ヘッダーで定義
<typeinfo>
|
||
|
class
bad_cast
:
public
std::
exception
|
||
この型の例外は、参照型への dynamic_cast が実行時チェックに失敗した場合(例えば、型が継承関係にないため)、およびロケールに要求されたファセットが存在しない場合に std::use_facet からスローされます。
継承図
目次 |
メンバー関数
|
(constructor)
|
新しい
bad_cast
オブジェクトを構築する
(public member function) |
|
operator=
|
bad_cast
オブジェクトを置き換える
(public member function) |
|
what
|
説明文字列を返す
(public member function) |
std::bad_cast:: bad_cast
| (1) | ||
|
bad_cast
(
)
throw
(
)
;
|
(C++11まで) | |
|
bad_cast
(
)
noexcept
;
|
(C++11以降)
(constexpr C++26以降) |
|
| (2) | ||
|
bad_cast
(
const
bad_cast
&
other
)
throw
(
)
;
|
(C++11まで) | |
|
bad_cast
(
const
bad_cast
&
other
)
noexcept
;
|
(C++11以降)
(constexpr C++26以降) |
|
bad_cast
オブジェクトを構築します。実装定義のnull終端バイト文字列が含まれ、これは
what()
を通じてアクセス可能です。
std::bad_cast
を持つ場合、
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
となります。
(C++11以降)
パラメータ
| other | - | コピーする別の例外オブジェクト |
std::bad_cast:: operator=
|
bad_cast
&
operator
=
(
const
bad_cast
&
other
)
throw
(
)
;
|
(C++11まで) | |
|
bad_cast
&
operator
=
(
const
bad_cast
&
other
)
noexcept
;
|
(C++11以降)
(C++26以降 constexpr) |
|
other
の内容を代入します。
*
this
と
other
の両方が動的型
std::bad_cast
を持つ場合、代入後は
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
となります。
(C++11以降)
パラメータ
| other | - | 代入する別の例外オブジェクト |
戻り値
* this
std::bad_cast:: what
|
virtual
const
char
*
what
(
)
const
throw
(
)
;
|
(C++11まで) | |
|
virtual
const
char
*
what
(
)
const
noexcept
;
|
(C++11以降)
(C++26以降 constexpr) |
|
説明文字列を返します。
戻り値
説明情報を含む実装定義のナル終端文字列へのポインタ。この文字列は std::wstring への変換と表示に適しています。ポインタは、少なくとも取得元の例外オブジェクトが破棄されるまで、または例外オブジェクトの非constメンバ関数(コピー代入演算子など)が呼び出されるまで有効であることが保証されます。
|
定数評価中、返される文字列は通常のリテラルエンコーディングでエンコードされます。 |
(C++26以降) |
注記
実装は
what()
をオーバーライドすることが許可されていますが、必須ではありません。
std::exception から継承
メンバ関数
|
[virtual]
|
例外オブジェクトを破棄
(
std::exception
の仮想公開メンバ関数)
|
|
[virtual]
|
説明文字列を返す
(
std::exception
の仮想公開メンバ関数)
|
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions
|
202411L
|
(C++26) | constexpr 例外型のためのconstexpr |
例
#include <iostream> #include <typeinfo> struct Foo { virtual ~Foo() {} }; struct Bar { virtual ~Bar() { std::cout << "~Bar\n"; } }; struct Pub : Bar { ~Pub() override { std::cout << "~Pub\n"; } }; int main() { Pub pub; try { [[maybe_unused]] Bar& r1 = dynamic_cast<Bar&>(pub); // OK, アップキャスト [[maybe_unused]] Foo& r2 = dynamic_cast<Foo&>(pub); // 例外をスロー } catch (const std::bad_cast& e) { std::cout << "e.what(): " << e.what() << '\n'; } }
出力例:
e.what(): std::bad_cast ~Pub ~Bar