Namespaces
Variants

std::bad_typeid

ja.cppreference.net より
< cpp | 型
 
 
ユーティリティライブラリ
言語サポート
型サポート (基本型、RTTI)
ライブラリ機能テストマクロ (C++20)
プログラムユーティリティ
可変個引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)(C++20)(C++20)    
(C++20)
Swap と 型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
共通語彙型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
ヘッダ <typeinfo>
class bad_typeid : public std::exception;

で定義されます。この型の例外は、多相型の間接参照されたヌルポインタ値に対して typeid 演算子が適用された場合にスローされます。

cpp/error/exception

継承図

メンバー関数

(constructor)
新しい bad_typeid オブジェクトを構築する
(public member function)
operator=
bad_typeid オブジェクトを置き換える
(public member function)
what
説明文字列を返す
(public member function)

std::bad_typeid:: bad_typeid

(1)
bad_typeid ( ) throw ( ) ;
(C++11まで)
bad_typeid ( ) noexcept ;
(C++11から)
(constexprはC++26から)
(2)
bad_typeid ( const bad_typeid & other ) throw ( ) ;
(C++11まで)
bad_typeid ( const bad_typeid & other ) noexcept ;
(C++11から)
(constexprはC++26から)

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

1) デフォルトコンストラクタ。
2) コピーコンストラクタ。 * this と other の両方が動的型 std::bad_typeid を持つ場合、 std:: strcmp ( what ( ) , other. what ( ) ) == 0 となります。 (C++11から)

パラメータ

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

std::bad_typeid::operator=

bad_typeid& operator=( const bad_typeid& other ) throw();
(C++11まで)
bad_typeid& operator=( const bad_typeid& other ) noexcept;
(C++11以降)

otherの内容を自身に代入します。 *thisとotherの両方が動的な型std::bad_typeidを持つ場合、代入後はstd::strcmp(what(), other.what()) == 0となります。(C++11以降)

パラメータ

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

戻り値

* this

std::bad_typeid::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 から継承

メンバ関数

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

注記

機能テスト マクロ 値 標準 機能
__cpp_lib_constexpr_exceptions 202411L (C++26) constexpr 例外型のためのconstexpr

例

#include <iostream>
#include <typeinfo>

struct S // The type has to be polymorphic
{
    virtual void f();
};

int main()
{
    S* p = nullptr;
    try
    {
        std::cout << typeid(*p).name() << '\n';
    }
    catch (const std::bad_typeid& e)
    {
        std::cout << e.what() << '\n';
    }
}

出力例:

Attempted a typeid of NULL pointer!