std:: bad_any_cast
|
定义于头文件
<any>
|
||
|
class
bad_any_cast
:
public
std::
bad_cast
|
(C++17 起) | |
定义了一种对象类型,当 std::any_cast 的值返回形式执行失败时会抛出该类型对象。
目录 |
成员函数
|
(constructor)
|
构造新的
bad_any_cast
对象
(公开成员函数) |
|
operator=
|
替换
bad_any_cast
对象
(公开成员函数) |
|
what
|
返回说明性字符串
(公开成员函数) |
std::bad_any_cast:: bad_any_cast
|
bad_any_cast
(
)
noexcept
;
|
(1) | (since C++17) |
|
bad_any_cast
(
const
bad_any_cast
&
other
)
noexcept
;
|
(2) | (since C++17) |
构造一个新的
bad_any_cast
对象,该对象包含一个实现定义的以空字符结尾的字节字符串,可通过
what()
访问。
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
;
|
(since 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
;
|
(since C++17) | |
返回解释性字符串。
返回值
指向实现定义的以空字符结尾的字符串的指针,该字符串包含解释性信息。该字符串适合转换并显示为 std::wstring 。该指针保证至少在获取它的异常对象被销毁之前,或者在调用异常对象上的非const成员函数(例如拷贝赋值运算符)之前保持有效。
注解
实现允许但不要求重写
what()
。
继承自 std:: bad_cast
继承自 std:: exception
成员函数
|
[virtual]
|
销毁异常对象
(
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); // 正常 try { [[maybe_unused]] auto s = std::any_cast<std::string>(x); // 抛出异常 } catch (const std::bad_any_cast& ex) { std::println("{}", ex.what()); } }
可能的输出:
bad any_cast