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 起) |
- *lhs == *rhs
- lhs.error() == rhs.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())
[编辑] 异常
当比较抛出异常时,此函数也会抛出相同的异常。
[编辑] 注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__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 << "Overload (1):\n" << e1.value() << (e1 == e2 ? " == " : " != ") << *e2 << '\n' << e1.value() << (e1 != e3 ? " != " : " == ") << *e3 << "\n\n"; std::cout << "Overload (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 << "Overload (3):\n" << *e1 << (e1 == x1 ? " == " : " != ") << x1 << '\n' << *e1 << (e1 != x2 ? " != " : " == ") << x2 << "\n\n"; }
输出
Overload (1): 💚 == 💚 💚 != ❌ Overload (2): 💚 != 13 13 == 13 31 != 13 Overload (3): 💚 == 💚 💚 != ❌
[编辑] 参见
(C++23) |
表示为不可预期值 (类模板) |