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 ( ) )
异常
当比较操作抛出异常时,抛出相应的异常类型。
注释
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__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
对象
(函数模板) |