operator==(std::expected)
主模板 |
||
template< class T2, class E2 > requires (!std::is_void_v<T2>) |
(1) | (C++23 起) |
template< class E2 > friend constexpr bool operator==( const expected& lhs, |
(2) | (C++23 起) |
template< class T2 > friend constexpr bool operator==( const expected& lhs, const T2& val ); |
(3) | (C++23 起) |
void 偏特化 |
||
template< class T2, class E2 > requires std::is_void_v<T2> |
(4) | (C++23 起) |
template< class E2 > friend constexpr bool operator==( const expected& lhs, |
(5) | (C++23 起) |
對 std::expected 物件執行比較操作。
如果以下任何表示式格式不正確,或其結果不可轉換為 bool,則程式格式不正確。 |
(直到 C++26) |
此過載僅在以下所有表示式格式正確且其結果可轉換為 bool 時才參與過載決議。 |
(C++26 起) |
- *lhs == *rhs
- lhs.error() == rhs.error()
如果表示式 lhs.error() == unex.error() 格式不正確,或其結果不可轉換為 bool,則程式格式不正確。 |
(直到 C++26) |
此過載僅在表示式 lhs.error() == unex.error() 格式正確且其結果可轉換為 bool 時才參與過載決議。 |
(C++26 起) |
如果表示式 *lhs == val 格式不正確,或其結果不可轉換為 bool,則程式格式不正確。 |
(直到 C++26) |
僅當所有以下條件都滿足時,此過載才參與過載決議:
|
(C++26 起) |
如果表示式 lhs.error() == rhs.error() 格式不正確,或其結果不可轉換為 bool,則程式格式不正確。 |
(直到 C++26) |
此過載僅在表示式 lhs.error() == rhs.error() 格式正確且其結果可轉換為 bool 時才參與過載決議。 |
(C++26 起) |
如果表示式 lhs.error() == unex.error() 格式不正確,或其結果不可轉換為 bool,則程式格式不正確。 |
(直到 C++26) |
此過載僅在表示式 lhs.error() == unex.error() 格式正確且其結果可轉換為 bool 時才參與過載決議。 |
(C++26 起) |
這些函式對普通的非限定查詢或限定查詢不可見,只有當 std::expected<T, E>
是引數的關聯類時,才能透過實參依賴查詢找到它們。
!=
運算子由 operator==
合成。
目錄 |
[編輯] 引數
lhs, rhs | - | 要比較的 std::expected 物件。 |
unex | - | 要與 lhs 比較的 std::unexpected 值。 |
val | - | 要與 lhs 中包含的期望值進行比較的值。 |
[編輯] 返回值
(lhs.has_value() ? *lhs == *rhs : lhs.error() == rhs.error())
lhs.has_value() || static_cast<bool>(lhs.error() == rhs.error())
[編輯] 異常
當且僅當比較操作丟擲異常時,此函式才丟擲異常。
[編輯] 註解
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_constrained_equality |
202411L |
(C++26) | std::expected 的受限比較運算子 |
[編輯] 示例
#include <expected> #include <iostream> #include <string_view> using namespace std::string_view_literals; int main() { auto x1{"\N{GREEN HEART}"sv}; auto x2{"\N{CROSS MARK}"sv}; std::expected<std::string_view, int> e1{x1}, e2{x1}, e3{x2}; std::unexpected u1{13}; std::cout << "Overload (1):\n" << e1.value() << (e1 == e2 ? " == " : " != ") << *e2 << '\n' << e1.value() << (e1 != e3 ? " != " : " == ") << *e3 << "\n\n"; std::cout << "Overload (2):\n" << e1.value() << (e1 == u1 ? " == " : " != ") << u1.error() << '\n'; e1 = std::unexpected{13}; std::cout << e1.error() << (e1 == u1 ? " == " : " != ") << u1.error() << '\n'; e1 = std::unexpected{31}; std::cout << e1.error() << (e1 != u1 ? " != " : " == ") << u1.error() << '\n'; std::cout << "Overload (3):\n" << *e1 << (e1 == x1 ? " == " : " != ") << x1 << '\n' << *e1 << (e1 != x2 ? " != " : " == ") << x2 << "\n\n"; }
輸出
Overload (1): 💚 == 💚 💚 != ❌ Overload (2): 💚 != 13 13 == 13 31 != 13 Overload (3): 💚 == 💚 💚 != ❌
[編輯] 參閱
(C++23) |
表示為意外值 (類模板) |