命名空间
变体
操作

std::unexpected

来自 cppreference.cn
< cpp‎ | 工具库‎ | expected
 
 
 
 
定义于头文件 <expected>
template< class E >
class unexpected;
(C++23 起)

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

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

目录

[edit] 模板参数

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

[edit] 成员函数

构造 unexpected 对象
(公开成员函数)
(析构函数)
(隐式声明)
销毁 unexpected 对象及其存储的值
(公开成员函数)
operator=
(隐式声明)
赋值存储的值
(公开成员函数)
访问存储的值
(公开成员函数)
交换存储的值
(公开成员函数)

[edit] 非成员函数

比较存储的值
(函数模板)
特化 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) 构造存储的值,如同通过 直接初始化 类型为 E 的值,从 std::forward<Err>(e) 构造。
4) 构造存储的值,如同通过 直接初始化 类型为 E 的值,从参数 std::forward<Args>(args)... 构造。
5) 构造存储的值,如同通过 直接初始化 类型为 E 的值,从参数 il, std::forward<Args>(args)... 构造。

参数

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> 是参数的关联类时才能通过 参数依赖查找 找到。

[edit] 推导指南

template< class E >
unexpected(E) -> unexpected<E>;
(C++23 起)

unexpected 提供了 推导指南,以允许从构造函数参数进行推导。

[edit] 注记

在 C++17 之前,名称 std::unexpected 表示当动态异常规范被违反时,C++ 运行时调用的函数。

[edit] 示例

#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

[edit] 参阅

构造 expected 对象
(public member function) [编辑]
比较 expected 对象
(function template) [编辑]