命名空间
变体
操作

std::bad_optional_access

来自 cppreference.cn
< cpp‎ | utility‎ | optional
 
 
 
 
定义于头文件 <optional>
class bad_optional_access;
(自 C++17 起)

定义了当访问不含值的 optional 对象时,由 std::optional::value 抛出的对象类型。

cpp/error/exceptionstd-bad optional access-inheritance-lwg2806.svg

继承关系图

内容

[编辑] 成员函数

(构造函数)
构造新的 bad_optional_access 对象
(公有成员函数)
operator=
替换 bad_optional_access 对象
(公有成员函数)
what
返回解释性字符串
(公有成员函数)

std::bad_optional_access::bad_optional_access

bad_optional_access() noexcept;
(1) (自 C++17 起)
bad_optional_access( const bad_optional_access& other ) noexcept;
(2) (自 C++17 起)

构造新的 bad_optional_access 对象,它带有一个实现定义的空终止字节字符串,该字符串可通过 what() 访问。

1) 默认构造函数。
2) 复制构造函数。 如果 *thisother 均具有动态类型 std::bad_optional_access,则 std::strcmp(what(), other.what()) == 0

参数

other - 要复制的另一个异常对象

std::bad_optional_access::operator=

bad_optional_access& operator=( const bad_optional_access& other ) noexcept;
(自 C++17 起)

other 的内容赋值。如果 *thisother 均具有动态类型 std::bad_optional_access,则赋值后 std::strcmp(what(), other.what()) == 0

参数

other - 要赋值的另一个异常对象

返回值

*this

std::bad_optional_access::what

virtual const char* what() const noexcept;
(自 C++17 起)

返回解释性字符串。

返回值

指向实现定义的空终止字符串的指针,其中包含解释性信息。该字符串适合转换为并显示为 std::wstring。指针保证有效,至少到从中获取它的异常对象被销毁为止,或者直到调用异常对象上的非 const 成员函数(例如,复制赋值运算符)为止。

返回的字符串在使用常量求值期间使用普通字面量编码进行编码。

(自 C++26 起)

注解

允许但不是必须实现 what() 的重载。

继承自 std::exception

成员函数

[虚函数]
销毁异常对象
(std::exception 的虚公有成员函数) [编辑]
[虚函数]
返回解释性字符串
(std::exception 的虚公有成员函数) [编辑]