operator== (std::expected)
Primary template
|
||
template
<
class
T2,
class
E2
>
requires
(
!
std::
is_void_v
<
T2
>
)
|
(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,
|
(3) | (since C++23) |
void
partial specialization
|
||
template
<
class
T2,
class
E2
>
requires
std::
is_void_v
<
T2
>
|
(4) | (since C++23) |
template
<
class
E2
>
friend
constexpr
bool
operator
==
(
const
expected
&
x,
|
(5) | (since C++23) |
Performs comparison operations on
expected
objects.
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.
- * x == * y
- x. error ( ) == y. error ( )
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
.
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
(
)
.
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.
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
(
)
.
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
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
(C++23)
|
represented as an unexpected value
(class template) |