Namespaces
Variants

std:: bad_any_cast

From cppreference.net
Utilities library
定義済みヘッダー <any>
class bad_any_cast : public std:: bad_cast
(C++17以降)

std::any_cast の値返却形式が失敗した際にスローされるオブジェクトの型を定義します。

目次

メンバー関数

(コンストラクタ)
新しい bad_any_cast オブジェクトを構築する
(公開メンバ関数)
operator=
bad_any_cast オブジェクトを置き換える
(公開メンバ関数)
what
説明文字列を返す
(公開メンバ関数)

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以降)

新しい bad_any_cast オブジェクトを構築します。実装定義のnull終端バイト文字列を含み、 what() を通じてアクセス可能です。

1) デフォルトコンストラクタ。
2) コピーコンストラクタ。 * this other の両方が動的型 std::bad_any_cast である場合、 std:: strcmp ( what ( ) , other. what ( ) ) == 0 となります。

パラメータ

other - コピーする別の例外オブジェクト

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 となります。

パラメータ

other - 代入する別の例外オブジェクト

戻り値

* this

std::bad_any_cast:: what

virtual const char * what ( ) const noexcept ;
(C++17以降)

説明文字列を返します。

戻り値

説明情報を含む実装定義のナル終端文字列へのポインタ。この文字列は std::wstring への変換と表示に適しています。ポインタは、少なくとも取得元の例外オブジェクトが破棄されるまで、または例外オブジェクトの非constメンバー関数(コピー代入演算子など)が呼び出されるまで有効であることが保証されます。

注記

実装は what() をオーバーライドすることが許可されていますが、必須ではありません。

std:: bad_cast から継承

std::exception から継承 std:: exception

メンバ関数

例外オブジェクトを破棄
( std::exception の仮想公開メンバ関数)
[virtual]
説明文字列を返す
( std::exception の仮想公開メンバ関数)

#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