operator==(std::expected)
来自 zh.cppreference.net
| 主模板 |
||
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 都包含相等的期望值,或者都包含相等的非期望值时,它们才比较相等。
|
如果以下任何表达式非良构,或其结果不可转换为 |
(C++26 前) |
|
仅当以下所有表达式良构且其结果可转换为 |
(C++26 起) |
*lhs == *rhslhs.error() == rhs.error()
2) 将 std::expected 对象与 std::unexpected 对象进行比较。当且仅当
lhs 包含的非期望值等于 unex.error() 时,它们才比较相等。
|
如果表达式 |
(C++26 前) |
|
仅当表达式 |
(C++26 起) |
3) 将 std::expected 对象与期望值进行比较。当且仅当
lhs 包含的期望值等于 val 时,它们才比较相等。
|
如果表达式 |
(C++26 前) |
|
仅当满足以下所有条件时,此重载才参与重载决议:
|
(C++26 起) |
4) 比较两个 std::expected 对象。当且仅当
lhs 和 rhs 都表示期望值,或者都包含相等的非期望值时,它们才比较相等。
|
如果表达式 |
(C++26 前) |
|
仅当表达式 |
(C++26 起) |
5) 将 std::expected 对象与 std::unexpected 对象进行比较。当且仅当
lhs 包含的非期望值等于 unex.error() 时,它们才比较相等。
|
如果表达式 |
(C++26 前) |
|
仅当表达式 |
(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 ( ) )
( 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 ( ) )
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
对象
(函数模板) |