Namespaces
Variants

operator== (std::expected)

From cppreference.net
Utilities library
主模板
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 对象。当且仅当 lhs rhs 同时包含相等的期望值,或同时包含相等的非期望值时,这两个对象才被视为相等。

若以下任一表达式非法,或其结果不可转换为 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 对象。当且仅当 lhs rhs 同时包含期望值,或同时包含相等的非期望值时,这两个对象才被视为相等。

若表达式 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 << "重载 (1):\n"
              << e1.value() << (e1 == e2 ? " == " : " != ") << *e2 << '\n'
              << e1.value() << (e1 != e3 ? " != " : " == ") << *e3 << "\n\n";
    std::cout << "重载 (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 << "重载 (3):\n"
              << *e1 << (e1 == x1 ? " == " : " != ") << x1 << '\n'
              << *e1 << (e1 != x2 ? " != " : " == ") << x2 << "\n\n";
}

输出:

重载 (1):
💚 == 💚
💚 != ❌
重载 (2):
💚 != 13
13 == 13
31 != 13
重载 (3):
💚 == 💚
💚 != ❌

参见

(C++23)
比较 std::unexpected 对象
(函数模板)