std::bad_expected_access

来自cppreference.com
< cpp‎ | utility‎ | 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<E> 存储了非预期值的副本。
2) bad_expected_access<void> 是所有其他 bad_expected_access 特化的基类。
std::bad_expected_access 的全部成员函数均为 constexpr:在常量表达式求值中创建并使用 std::bad_expected_access 对象是可能的。

然而,std::bad_expected_access 对象通常不能为 constexpr,因为任何动态分配的存储都必须在相同的常量表达式求值中释放。

(C++26 起)

目录

[编辑] 主模板的成员

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

std::bad_expected_access::bad_expected_access

explicit bad_expected_access( E e );
(C++23 起)
(C++26 起为 constexpr)

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

std::bad_expected_access::error

const E& error() const & noexcept;
(1) (C++23 起)
(C++26 起为 constexpr)
E& error() & noexcept;
(2) (C++23 起)
(C++26 起为 constexpr)
const E&& error() const && noexcept;
(3) (C++23 起)
(C++26 起为 constexpr)
E&& error() && noexcept;
(4) (C++23 起)
(C++26 起为 constexpr)

返回到储存的值的引用。

std::bad_expected_access::what

const char* what() const noexcept override;
(C++23 起)
(C++26 起为 constexpr)

返回用于解释的字符串。

返回值

指向一个包含解释性信息的空终止字符串。该字符串可以转换和显示为 std::wstring。该指针保证在至少从其获得的异常对象被销毁之前,或者在异常对象上调用一个非 const 成员函数(例如复制赋值运算符)之前,是有效的。

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

(C++26 起)

注解

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

继承自 std::bad_expected_access<void>

bad_expected_access<void> 特化的成员

(构造函数)
构造 bad_expected_access<void> 对象
(受保护成员函数)
(析构函数)
销毁 bad_expected_access<void> 对象
(受保护成员函数)
operator=
替换 bad_expected_access<void> 对象
(受保护成员函数)
what
返回用于解释的字符串
(公开成员函数)

bad_expected_access<void> 特化的成员函数是受保护的,它们只能在派生类中调用。

继承自 std::exception

成员函数

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

[编辑] 注解

功能特性测试 标准 功能特性
__cpp_lib_constexpr_exceptions 202502L (C++26) constexpr std::bad_expected_access

[编辑] 示例

[编辑] 参阅

指示进行了到不含值的 optional 的有检查访问的异常
(类) [编辑]
非法地访问 variant 的值时抛出的异常
(类) [编辑]