operator==(std::expected)
来自cppreference.com
| 主模板 |
||
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 起) |
*x == *yx.error() == y.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())
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) |
表示一个非预期值 (类模板) |