名稱空間
變體
操作

std::unexpected

來自 cppreference.com
< 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) [編輯]