operator==(std::expected)
主模板 |
||
template< class T2, class E2 > requires (!std::is_void_v<T2>) |
(1) | (C++23 起) |
template< class E2 > friend constexpr bool operator==( const expected& lhs, |
(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> |
(4) | (C++23 起) |
template< class E2 > friend constexpr bool operator==( const expected& lhs, |
(5) | (C++23 起) |
对 std::expected 对象执行比较操作。
如果以下任意表达式非良构或它(们)的结果不可转换到 bool,那么程序非良构: |
(C++26 前) |
此重载只有在以下所有表达式都良构且它们的结果可转换到 bool 时才会参与重载决议: |
(C++26 起) |
- *x == *y
- x.error() == y.error()
如果表达式 lhs.error() == unex.error() 非良构或它的结果不可转换到 bool,那么程序非良构。 |
(C++26 前) |
此重载只有在表达式 lhs.error() == unex.error() 良构且它的结果可转换到 bool 时才会参与重载决议。 |
(C++26 起) |
如果表达式 *lhs == val 非良构或它的结果不可转换到 bool,那么程序非良构。 |
(C++26 前) |
此重载只有在满足以下所有条件时才会参与重载决议:
|
(C++26 起) |
如果表达式 lhs.error() == rhs.error() 非良构或它的结果不可转换到 bool,那么程序非良构。 |
(C++26 前) |
此重载只有在表达式 lhs.error() == rhs.error() 良构且它的结果可转换到 bool 时才会参与重载决议。 |
(C++26 起) |
如果表达式 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 中包含的预期值比较的值 |
[编辑] 返回值
(lhs.has_value() ? *lhs == *rhs : lhs.error() == rhs.error())
lhs.has_value() || static_cast<bool>(lhs.error() == rhs.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) |
表示一个非预期值 (类模板) |