Namespaces
Variants

std:: bad_expected_access

From cppreference.net
Utilities library
定义于头文件 <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) 定义了一种对象类型,当访问包含非预期值的 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 的,因为任何动态分配的存储必须在同一常量表达式求值过程中释放。

(since C++26)

目录

主模板的成员

(constructor)
构造 bad_expected_access 对象
(公开成员函数)
error
返回存储的值
(公开成员函数)
what
返回说明性字符串
(公开成员函数)

std::bad_expected_access:: bad_expected_access

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

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

std::bad_expected_access:: error

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

返回对存储值的引用。

std::bad_expected_access:: what

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

返回解释性字符串。

返回值

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

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

(since C++26)

注解

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

继承自 std:: bad_expected_access < void >

bad_expected_access < void > 特化的成员

(constructor)
构造 bad_expected_access < void > 对象
(受保护成员函数)
(destructor)
销毁 bad_expected_access < void > 对象
(受保护成员函数)
operator=
替换 bad_expected_access < void > 对象
(受保护成员函数)
what
返回说明性字符串
(公开成员函数)

bad_expected_access < void > 的特殊成员函数均为受保护。它们只能被派生类调用。

继承自 std:: exception

成员函数

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

注释

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

示例

参见

指示对不包含值的 optional 对象进行检查式访问时抛出的异常
(类)
当对 variant 值进行无效访问时抛出的异常
(类)