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> 為 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 | - | 初始化所含值所用的初始化式列表 |
異常
拋出任何 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> 為 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 對象 (公開成員函數) | |
(C++23) |
比較 expected 對象 (函數模板) |