std::unexpected
| 在标头  <expected>定义 | ||
| template< class E > class unexpected; | ||
类模板 std::unexpected 代表一个 std::expected 中存储的不期待的值。特别地, std::expected 有一个接受 std::unexpected 为唯一参数的构造函数,该构造函数创建一个含有不期待值的 expected 对象。
用非对象类型,数组类型, std::unexpected 的特化或有 cv 限定的类型实例化 unexpected 的程序非良构。
模板形参
| E | - | 不期待的值的类型,该类型不能是非对象类型,数组类型, std::unexpected的特化或有 cv 限定的类型 | 
成员函数
|  (构造函数) | 构造 unexpected对象(公开成员函数) | 
| (析构函数) (隐式声明) | 销毁 unexpected对象以及其中存储的值(公开成员函数) | 
| operator= (隐式声明) | 向存储的值赋值 (公开成员函数) | 
|  error | 获取存储的值 (公开成员函数) | 
|  swap | 与存储的值交换 (公开成员函数) | 
非成员函数
|  operator== | 与存储的值比较 (函数模板) | 
|  swap(std::unexpected) (C++23) | 特化 std::swap 算法 (函数模板) | 
std::unexpected::unexpected
| constexpr unexpected( const unexpected& ) = default; | (1) | (C++23 起) | 
| constexpr unexpected( unexpected&& ) = default; | (2) | (C++23 起) | 
| template< class Err = E > constexpr explicit unexpected( Err&& e ); | (3) | (C++23 起) | 
| template< class... Args > constexpr explicit unexpected( std::in_place_t, Args&&... args ); | (4) | (C++23 起) | 
| template< class U, class... Args > constexpr explicit unexpected( std::in_place_t, | (5) | (C++23 起) | 
构造 std::unexpected 对象。
E 的值一样构造存储的值。
-  此重载只有在
- std::is_same_v<std::remove_cvref_t<Err>, unexpected> 为 false ,且
- std::is_same_v<std::remove_cvref_t<Err>, std::in_place_t> 为 false ,且
- std::is_constructible_v<E, Err> 为 true 时才参与重载决议。
 
E 的值一样构造存储的值。
- 此重载只有在 std::is_constructible_v<E, Args...> 为 true 时才参与重载决议。
E 的值一样构造存储的值。
- 此重载只有在 std::is_constructible_v<E, std::initializer_list<U>&, Args...> 为 true 时才参与重载决议。
参数
| e | - | 初始化所含值所用的值 | 
| args... | - | 初始化所含值所用的参数 | 
| il | - | 初始化所含值所用的 initializer_list | 
异常
抛出任何 E 的构造函数所抛的异常。
std::unexpected::error
| constexpr const E& error() const& noexcept; constexpr E& error() & noexcept; | (C++23 起) | |
返回到存储的值的引用。
std::unexpected::swap
| constexpr void swap( unexpected& other ) noexcept(std::is_nothrow_swappable_v<E>); | (C++23 起) | |
如同 using std::swap; swap(error(), other.error()); 一样交换存储的值。
如果 std::is_swappable_v<E> 为 false ,程序非良构。
operator==(std::unexpected)
| template< class E2 > friend constexpr bool operator==( unexpected& x, std::unexpected<E2>& y ); | (C++23 起) | |
如同 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))); | (C++23 起) | |
等价于 x.swap(y) 。
此重载只有在std::is_swappable_v<E> 为 true 时才参与重载决议。
此函数对通常无限定或有限定查找不可见,而只能在 std::unexpected<E> 为参数的关联类时由实参依赖查找找到。
推导指引
| template< class E > unexpected(E) -> unexpected<E>; | (C++23 起) | |
为 unexpected 提供推导指引以允许从构造函数参数推导模板实参。
注解
在 C++17 前,名字 std::unexpected 指代 C++ 运行时在违背动态异常规范时调用的函数。
示例
#include <expected> #include <iostream> 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"; } }
输出:
ex contains an error value The error value is equal to 3
参阅
| (C++23) | 构造 expected对象(公开成员函数) | 
| (C++23) | 比较 expected对象(函数模板) |