名前空間
変種
操作

std::bad_typeid

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

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 
ヘッダ <typeinfo> で定義
class bad_typeid : public std::exception;

多態型へのヌルポインタ値に対する typeid 演算子の適用時に、この型の例外がスローされます。

cpp/error/exceptionstd-bad typeid-inheritance.svg

継承図

目次

[編集] メンバ関数

(コンストラクタ)
新しい 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以降)
(2)
bad_typeid( const bad_typeid& other ) throw();
(C++11まで)
bad_typeid( const bad_typeid& other ) noexcept;
(C++11以降)

what() 経由でアクセス可能な、実装定義のヌル終端バイト文字列で新しい bad_typeid オブジェクトを構築します。

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

パラメータ

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

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 の内容を代入します。もし *thisother が両方とも動的型 std::bad_typeid を持つ場合、代入後の std::strcmp(what(), other.what()) == 0 となります。(C++11 以降)

パラメータ

その他 - 割り当てる別の例外オブジェクト

戻り値

*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 の仮想 public メンバー関数) [編集]
[virtual]
説明文字列を返す
(std::exception の仮想 public メンバー関数) [編集]

[編集] 注記

機能テストマクロ 規格 機能
__cpp_lib_constexpr_exceptions 202411L (C++26) constexpr for exception types

[編集]

#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!
English 日本語 中文(简体) 中文(繁體)