operator==(std::expected)

From cppreference.com
< cpp‎ | utility‎ | expected
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Primary template
template< class T2, class E2 >

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

                                  const std::expected<T2, E2>& y );
(1) (since C++23)
template< class T2 >
friend constexpr bool operator==( const expected& x, const T2& val );
(2) (since C++23)
template< class E2 >

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

                                  const std::unexpected<E2>& e );
(3) (since C++23)
void partial specialization
template< class T2, class E2 >

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

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

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

                                  const std::unexpected<E2>& e );
(5) (since C++23)

Performs comparison operations on expected objects.

1) Compares two expected objects. The objects compare equal if and only if both x and y contain expected values that are equal, or both contain unexpected values that are equal.
If any of the following expressions is ill-formed, or its result is not convertible to bool, the program is ill-formed:
  • *x == *y
  • x.error() == y.error()
2) Compares expected object with an expected value. The objects compare equal if and only if x contains an expected value, and the expected value is equal to val.
If the expression *x == val is not well-formed, or its result is not convertible to bool, the program is ill-formed.
3) Compares expected object with an unexpected value. The objects compare equal if and only if x contains an unexpected value, and the unexpected value is equal to e.error().
If the expression x.error() == e.error() is not well-formed, or its result is not convertible to bool, the program is ill-formed.
4) Compares two expected objects. The objects compare equal if and only if x and y both represent expected values, or both contain unexpected values that are equal.
If the expression x.error() == y.error() is not well-formed, or its result is not convertible to bool, the program is ill-formed.
5) Compares expected object with an unexpected value. The objects compare equal if and only if x contains an unexpected value, and the unexpected value is equal to e.error().
If the expression x.error() == e.error() is not well-formed, or its result is not convertible to bool, the program is ill-formed.

These functions are not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::expected<T, E> is an associated class of the arguments.

The != operator is synthesized from operator==.

Parameters

x, y - expected object to compare
val - value to compare to the expected value contained in x
e - value to compare to the unexpected value contained in x

Return value

1) x.has_value() ? (y.has_value() && *x == *y) : (!y.has_value() && x.error() == y.error())
2) x.has_value() && static_cast<bool>(*x == val)
3) !x.has_value() && static_cast<bool>(x.error() == e.error())
4) x.has_value() ? y.has_value() : (!y.has_value() && x.error() == y.error())
5) !x.has_value() && static_cast<bool>(x.error() == e.error())

Exceptions

Throws when and what the comparison throws.

Example

#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 << (e1 == x1 ? " == " : " != ") << x1 << '\n'
              << *e1 << (e1 != x2 ? " != " : " == ") << x2 << "\n\n";
 
    std::cout << "Overload (3):\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';
}

Output:

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

See also

represented as an unexpected value
(class template)