命名空间
变体
操作

operator==(std::expected)

来自 cppreference.cn
< cpp‎ | utility‎ | 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())

[编辑] 异常

当比较抛出异常时,此函数也会抛出相同的异常。

[编辑] 注解

特性测试 Std 特性
__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):
💚 == 💚
💚 != ❌

[编辑] 参见

表示为不可预期值
(类模板) [编辑]