Namespaces
Variants

std:: unexpected

From cppreference.net
Utilities library
定义于头文件 <expected>
template < class E >
class unexpected ;
(C++23 起)

类模板 std::unexpected 表示存储在 std::expected 中的意外值。具体而言, std::expected 包含以 std::unexpected 作为单一参数的构造函数,该构造函数会创建一个包含意外值的 expected 对象。

如果程序实例化 unexpected 时使用了非对象类型、数组类型、 std::unexpected 的特化版本或cv限定类型,则该程序属于错误格式。

目录

模板参数

E - 意外值的类型。该类型不能是数组类型、非对象类型、 std::unexpected 的特化或cv限定类型。

成员函数

构造 unexpected 对象
(公开成员函数)
(destructor)
(implicitly declared)
销毁 unexpected 对象及其存储的值
(公开成员函数)
operator=
(implicitly declared)
赋值存储的值
(公开成员函数)
访问存储的值
(公开成员函数)
交换存储的值
(公开成员函数)

非成员函数

(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 ,

std:: initializer_list < U > il, Args && ... args ) ;
(5)

构造一个 std::unexpected 对象。

1,2) 复制/移动构造函数。分别复制或移动存储的值。
3) 构造存储的值,如同通过 直接初始化 std:: forward < Err > ( e ) 初始化类型 E 的值。
4) 构造存储的值,如同通过 直接初始化 从参数 std:: forward < Args > ( args ) ... 初始化类型 E 的值。
5) 构造存储的值,如同通过 直接初始化 从参数 il, std:: forward < Args > ( args ) ... 初始化类型 E 的值。

参数

e - 用于初始化所含值的值
args... - 用于初始化所含值的参数
il - 用于初始化所含值的初始化列表

异常

抛出 E 的构造函数抛出的任何异常。

std::unexpected:: error

constexpr const E & error ( ) const & noexcept ;

constexpr E & error ( ) & noexcept ;
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 > 为 false,则程序非良构。

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 > 为 true 时参与重载决议。

此函数对普通的 非限定查找 限定查找 不可见,仅当 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 包含错误值\n";
    if (ex == std::unexpected(3))
        std::cout << "错误值等于 3\n";
    const auto e = unexpected_runtime_error();
    e.and_then([](const auto& e) -> std::expected<int, error>
    {
        std::cout << "and_then: " << int(e); // 不会输出
        return {};
    })
    .or_else([](const auto& e) -> std::expected<int, error>
    {
        std::cout << "or_else: " << int(e); // 输出此行
        return {};
    });
}

输出:

ex 包含错误值
错误值等于 3
or_else: 1

参见

构造 expected 对象
(公开成员函数)
返回期望值
(公开成员函数)
交换内容
(公开成员函数)
特化 std::swap 算法
(函数)
(C++23)
比较 expected 对象
(函数模板)