命名空间
变体
操作

std::bad_expected_access

来自 cppreference.cn
< cpp‎ | 工具库‎ | expected
 
 
 
 
定义于头文件 <expected>
template< class E >
class bad_expected_access : public std::bad_expected_access<void>
(1) (C++23 起)
template<>
class bad_expected_access<void> : public std::exception
(2) (C++23 起)
1) 定义了一个对象类型,当访问一个包含意外值的 `expected` 对象时,std::expected::value 会抛出该类型的对象。`bad_expected_access` 存储了意外值的一个副本。
2) `bad_expected_access` 是所有其他 `bad_expected_access` 特化类的基类。

目录

[编辑] 主模板的成员

(构造函数)
构造一个 `bad_expected_access` 对象
(公开成员函数)
error
返回存储的值
(公开成员函数)
what
返回解释字符串
(公开成员函数)

std::bad_expected_access::bad_expected_access

explicit bad_expected_access( E e );

构造一个新的 `bad_expected_access` 对象。用 `std::move(e)` 初始化存储的值。

std::bad_expected_access::error

const E& error() const & noexcept;

E& error() & noexcept;
const E&& error() const && noexcept;

E&& error() && noexcept;

返回对存储值的引用。

std::bad_expected_access::what

const char* what() const noexcept override;

返回解释字符串。

参数

(无)

返回值

指向以 null 结尾的字符串的指针,包含解释信息。该字符串适合转换为 std::wstring 并显示。指针保证至少在获取它的异常对象被销毁之前或对异常对象调用非 const 成员函数(例如,复制赋值运算符)之前有效。

注意

允许但不要求实现重写 what()

[编辑] bad_expected_access<void> 特化版的成员

(构造函数)
构造一个 `bad_expected_access` 对象
(受保护成员函数)
(析构函数)
销毁 `bad_expected_access` 对象
(受保护成员函数)
operator=
替换 `bad_expected_access` 对象
(受保护成员函数)
what
返回解释字符串
(公开成员函数)

`bad_expected_access` 的特殊成员函数是受保护的。它们只能由派生类调用。

[编辑] 示例