std::bad_typeid - cppreference.com
来自cppreference.com
class bad_typeid : public std::exception; |
||
此类型的异常在应用 typeid 运算符到多态类型的空指针值时抛出。
继承图
成员函数
构造新的 bad_typeid 对象 (公开成员函数) | |
替换 bad_typeid 对象 (公开成员函数) | |
| 返回解释字符串 (公开成员函数) |
std::bad_typeid::bad_typeid
bad_typeid() throw(); |
(1) | (C++11 前) |
bad_typeid() noexcept; |
(C++11 起) | |
bad_typeid( const bad_typeid& other ) throw(); |
(2) | (C++11 前) |
bad_typeid( const bad_typeid& other ) noexcept; |
(C++11 起) | |
构造新的拥有实现定义的空终止字节字符串的 bad_typeid 对象,字符串能通过 what() 访问。
1) 默认构造函数。
2) 复制构造函数。如果 *this 与 other 均拥有动态类型 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 的内容赋值。如果 *this 与 other 均拥有动态类型 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()。
注解
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions |
202411L |
(C++26) | constexpr 的异常类型
|
示例
#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!