名稱空間
變體
操作

operator==(std::expected)

來自 cppreference.com
< cpp‎ | 工具庫‎ | expected
 
 
 
 
主模板
template< class T2, class E2 >

    requires (!std::is_void_v<T2>)
friend constexpr bool operator==( const expected& lhs,

                                  const std::expected<T2, E2>& rhs );
(1) (C++23 起)
template< class E2 >

friend constexpr bool operator==( const expected& lhs,

                                  const std::unexpected<E2>& unex );
(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>
friend constexpr bool operator==( const expected& lhs,

                                  const std::expected<T2, E2>& rhs );
(4) (C++23 起)
template< class E2 >

friend constexpr bool operator==( const expected& lhs,

                                  const std::unexpected<E2>& unex );
(5) (C++23 起)

std::expected 物件執行比較操作。

1) 比較兩個 std::expected 物件。當且僅當 lhsrhs 都包含相等的值,或者都包含相等的意外值時,物件才比較相等。

如果以下任何表示式格式不正確,或其結果不可轉換為 bool,則程式格式不正確。

(直到 C++26)

此過載僅在以下所有表示式格式正確且其結果可轉換為 bool 時才參與過載決議。

(C++26 起)
  • *lhs == *rhs
  • lhs.error() == rhs.error()
2)std::expected 物件與 std::unexpected 物件進行比較。當且僅當 lhs 包含一個與 unex.error() 相等的意外值時,物件才比較相等。

如果表示式 lhs.error() == unex.error() 格式不正確,或其結果不可轉換為 bool,則程式格式不正確。

(直到 C++26)

此過載僅在表示式 lhs.error() == unex.error() 格式正確且其結果可轉換為 bool 時才參與過載決議。

(C++26 起)
3)std::expected 物件與期望值進行比較。當且僅當 lhs 包含一個與 val 相等的期望值時,物件才比較相等。

如果表示式 *lhs == val 格式不正確,或其結果不可轉換為 bool,則程式格式不正確。

(直到 C++26)

僅當所有以下條件都滿足時,此過載才參與過載決議:

  • T2 不是 std::expected 的特化。
  • 表示式 *lhs == val 格式正確,且其結果可轉換為 bool
(C++26 起)
4) 比較兩個 std::expected 物件。當且僅當 lhsrhs 都表示期望值,或者都包含相等的意外值時,物件才比較相等。

如果表示式 lhs.error() == rhs.error() 格式不正確,或其結果不可轉換為 bool,則程式格式不正確。

(直到 C++26)

此過載僅在表示式 lhs.error() == rhs.error() 格式正確且其結果可轉換為 bool 時才參與過載決議。

(C++26 起)
5)std::expected 物件與 std::unexpected 物件進行比較。當且僅當 lhs 包含一個與 unex.error() 相等的意外值時,物件才比較相等。

如果表示式 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 中包含的期望值進行比較的值。

[編輯] 返回值

1) lhs.has_value() != rhs.has_value() ? false :
    (lhs.has_value() ? *lhs == *rhs : lhs.error() == rhs.error())
2) !lhs.has_value() && static_cast<bool>(lhs.error() == unex.error())
3) lhs.has_value() && static_cast<bool>(*lhs == val)
4) lhs.has_value() != rhs.has_value() ? false :
    lhs.has_value() || static_cast<bool>(lhs.error() == rhs.error())
5) !lhs.has_value() && static_cast<bool>(lhs.error() == unex.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):
💚 == 💚
💚 != ❌

[編輯] 參閱

表示為意外值
(類模板) [編輯]