C++ Type_info 库 - bad_typeid
描述
它在空点的 typeid 上引发异常。
声明
以下是 std::bad_typeid 的声明。
C++98
class bad_typeid;
C++11
class bad_typeid;
参数
none
返回值
none
异常
No-throw guarantee − 这个成员函数从不抛出异常。
数据竞争
语言环境对象被修改。
示例
在下面的 std::bad_typeid 示例中。
#include <iostream>
#include <typeinfo>
struct S {
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!

