std::unexpected
定义于头文件 <expected> |
||
template< class E > class unexpected; |
(自 C++23 起) | |
类模板 std::unexpected
表示存储在 std::expected 中的意外值。 特别是,std::expected 具有以 std::unexpected
作为单个参数的构造函数,这会创建一个包含意外值的 expected
对象。
如果程序使用非对象类型、数组类型、std::unexpected
的特化或 cv 限定类型实例化 unexpected
,则程序是非良构的。
内容 |
[编辑] 模板参数
E | - | 意外值的类型。 该类型不得为数组类型、非对象类型、std::unexpected 的特化或 cv 限定类型。 |
[编辑] 成员函数
构造 unexpected 对象(公共成员函数) | |
(析构函数) (隐式声明) |
销毁 unexpected 对象,以及存储的值(公共成员函数) |
operator= (隐式声明) |
赋值存储的值 (公共成员函数) |
访问存储的值 (公共成员函数) | |
交换存储的值 (公共成员函数) |
[编辑] 非成员函数
(C++23) |
比较存储的值 (函数模板) |
(C++23) |
特化 std::swap 算法 (函数模板) |
std::unexpected::unexpected
constexpr unexpected( const unexpected& ) = default; |
(1) | |
constexpr unexpected( unexpected&& ) = default; |
(2) | |
template< class Err = E > constexpr explicit unexpected( Err&& e ); |
(3) | |
template< class... Args > constexpr explicit unexpected( std::in_place_t, Args&&... args ); |
(4) | |
template< class U, class... Args > constexpr explicit unexpected( std::in_place_t, |
(5) | |
构造一个 std::unexpected
对象。
E
的值一样。- 此重载仅在以下情况参与重载解析
- std::is_same_v<std::remove_cvref_t<Err>, unexpected> 为假,并且
- std::is_same_v<std::remove_cvref_t<Err>, std::in_place_t> 为假,并且
- std::is_constructible_v<E, Err> 为真。
E
的值一样。- 此重载仅在 std::is_constructible_v<E, Args...> 为真时参与重载解析。
E
的值一样。- 此重载仅在 std::is_constructible_v<E, std::initializer_list<U>&, Args...> 为真时参与重载解析。
参数
e | - | 用于初始化包含的值的值 |
args... | - | 用于初始化包含的值的参数 |
il | - | 用于初始化包含的值的初始化列表 |
异常
抛出 E
的构造函数抛出的任何异常。
std::unexpected::error
constexpr const E& error() const& noexcept; constexpr E& error() & noexcept; |
||
返回对存储值的引用。
std::unexpected::swap
constexpr void swap( unexpected& other ) noexcept(std::is_nothrow_swappable_v<E>); |
||
交换存储的值,如同通过 using std::swap; swap(error(), other.error());。
如果 std::is_swappable_v<E> 为假,则程序是非良构的。
operator==(std::unexpected)
template< class E2 > friend constexpr bool operator==( unexpected& x, std::unexpected<E2>& y ); |
||
比较存储的值,如同通过 return x.error() == y.error()。
如果表达式 x.error() == e.error() 不是良构的,或者如果其结果无法转换为 bool,则程序是非良构的。
此函数对于普通的非限定查找或限定查找不可见,并且只能通过当 std::unexpected<E> 是参数的关联类时的实参依赖查找找到。
swap(std::unexpected)
friend constexpr void swap( unexpected& x, unexpected& y ) noexcept(noexcept(x.swap(y))); |
||
等效于 x.swap(y)。
此重载仅在 std::is_swappable_v<E> 为真时参与重载解析。
此函数对于普通的非限定查找或限定查找不可见,并且只能通过当 std::unexpected<E> 是参数的关联类时的实参依赖查找找到。
[编辑] 推导指引
template< class E > unexpected(E) -> unexpected<E>; |
(自 C++23 起) | |
推导指引为 unexpected 提供,以允许从构造函数参数推导。
[编辑] 注解
在 C++17 之前,名称 std::unexpected 表示当动态异常规范被违反时,C++ 运行时调用的函数。
[编辑] 示例
#include <expected> #include <iostream> enum class error { compile_time_error, runtime_error }; [[nodiscard]] auto unexpected_runtime_error() -> std::expected<int, error> { return std::unexpected(error::runtime_error); } int main() { std::expected<double, int> ex = std::unexpected(3); if (!ex) std::cout << "ex contains an error value\n"; if (ex == std::unexpected(3)) std::cout << "The error value is equal to 3\n"; const auto e = unexpected_runtime_error(); e.and_then([](const auto& e) -> std::expected<int, error> { std::cout << "and_then: " << int(e); // not printed return {}; }) .or_else([](const auto& e) -> std::expected<int, error> { std::cout << "or_else: " << int(e); // prints this line return {}; }); }
输出
ex contains an error value The error value is equal to 3 or_else: 1
[编辑] 参见
构造 expected 对象(公共成员函数) | |
(C++23) |
比较 expected 对象(函数模板) |