operator==, !=, <, <=, >, >=, <=> (std::optional)
|
定义于头文件
<optional>
|
||
|
比较两个
optional
对象
|
||
|
template
<
class
T,
class
U
>
constexpr bool operator == ( const optional < T > & lhs, const optional < U > & rhs ) ; |
(1) | (自 C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator ! = ( const optional < T > & lhs, const optional < U > & rhs ) ; |
(2) | (自 C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator < ( const optional < T > & lhs, const optional < U > & rhs ) ; |
(3) | (自 C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator <= ( const optional < T > & lhs, const optional < U > & rhs ) ; |
(4) | (自 C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator > ( const optional < T > & lhs, const optional < U > & rhs ) ; |
(5) | (自 C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator >= ( const optional < T > & lhs, const optional < U > & rhs ) ; |
(6) | (自 C++17 起) |
|
template
<
class
T,
std::
three_way_comparable_with
<
T
>
U
>
constexpr
std::
compare_three_way_result_t
<
T, U
>
|
(7) | (自 C++20 起) |
|
比较一个
optional
对象与
nullopt
|
||
|
template
<
class
T
>
constexpr bool operator == ( const optional < T > & opt, std:: nullopt_t ) noexcept ; |
(8) | (自 C++17 起) |
|
template
<
class
T
>
constexpr bool operator == ( std:: nullopt_t , const optional < T > & opt ) noexcept ; |
(9) |
(自 C++17 起)
(直至 C++20) |
|
template
<
class
T
>
constexpr bool operator ! = ( const optional < T > & opt, std:: nullopt_t ) noexcept ; |
(10) |
(自 C++17 起)
(直至 C++20) |
|
template
<
class
T
>
constexpr bool operator ! = ( std:: nullopt_t , const optional < T > & opt ) noexcept ; |
(11) |
(自 C++17 起)
(直至 C++20) |
|
template
<
class
T
>
constexpr bool operator < ( const optional < T > & opt, std:: nullopt_t ) noexcept ; |
(12) |
(C++17 起)
(C++20 前) |
|
template
<
class
T
>
constexpr bool operator < ( std:: nullopt_t , const optional < T > & opt ) noexcept ; |
(13) |
(自 C++17 起)
(直至 C++20) |
|
template
<
class
T
>
constexpr bool operator <= ( const optional < T > & opt, std:: nullopt_t ) noexcept ; |
(14) |
(自 C++17 起)
(直至 C++20) |
|
template
<
class
T
>
constexpr bool operator <= ( std:: nullopt_t , const optional < T > & opt ) noexcept ; |
(15) |
(自 C++17 起)
(直至 C++20) |
|
template
<
class
T
>
constexpr bool operator > ( const optional < T > & opt, std:: nullopt_t ) noexcept ; |
(16) |
(自 C++17 起)
(直至 C++20 止) |
|
template
<
class
T
>
constexpr bool operator > ( std:: nullopt_t , const optional < T > & opt ) noexcept ; |
(17) |
(自 C++17 起)
(直至 C++20) |
|
template
<
class
T
>
constexpr bool operator >= ( const optional < T > & opt, std:: nullopt_t ) noexcept ; |
(18) |
(自 C++17 起)
(直至 C++20) |
|
template
<
class
T
>
constexpr bool operator >= ( std:: nullopt_t , const optional < T > & opt ) noexcept ; |
(19) |
(自 C++17 起)
(直至 C++20 前) |
|
template
<
class
T
>
constexpr
std::
strong_ordering
|
(20) | (自 C++20 起) |
|
比较
optional
对象与值
|
||
|
template
<
class
T,
class
U
>
constexpr bool operator == ( const optional < T > & opt, const U & value ) ; |
(21) | (自 C++17 起) |
|
template
<
class
U,
class
T
>
constexpr bool operator == ( const U & value, const optional < T > & opt ) ; |
(22) | (自 C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator ! = ( const optional < T > & opt, const U & value ) ; |
(23) | (自 C++17 起) |
|
template
<
class
U,
class
T
>
constexpr bool operator ! = ( const U & value, const optional < T > & opt ) ; |
(24) | (自 C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator < ( const optional < T > & opt, const U & value ) ; |
(25) | (C++17 起) |
|
template
<
class
U,
class
T
>
constexpr bool operator < ( const U & value, const optional < T > & opt ) ; |
(26) | (C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator <= ( const optional < T > & opt, const U & value ) ; |
(27) | (自 C++17 起) |
|
template
<
class
U,
class
T
>
constexpr bool operator <= ( const U & value, const optional < T > & opt ) ; |
(28) | (自 C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator > ( const optional < T > & opt, const U & value ) ; |
(29) | (自 C++17 起) |
|
template
<
class
U,
class
T
>
constexpr bool operator > ( const U & value, const optional < T > & opt ) ; |
(30) | (自 C++17 起) |
|
template
<
class
T,
class
U
>
constexpr bool operator >= ( const optional < T > & opt, const U & value ) ; |
(31) | (自 C++17 起) |
|
template
<
class
U,
class
T
>
constexpr bool operator >= ( const U & value, const optional < T > & opt ) ; |
(32) | (自 C++17 起) |
|
template
<
class
T,
std::
three_way_comparable_with
<
T
>
U
>
constexpr
std::
compare_three_way_result_t
<
T, U
>
|
(33) | (自 C++20 起) |
对
optional
对象执行比较操作。
optional
对象
lhs
和
rhs
。仅当
lhs
和
rhs
均包含值时,才会比较所含值(使用
T
的对应运算符)。否则:
- lhs 被视为 等于 rhs 当且仅当 lhs 和 rhs 均不包含值。
- lhs 被视为 小于 rhs 当且仅当 rhs 包含值而 lhs 不包含值。
|
若对应的表达式 * lhs @ * rhs 非良构或其结果不可转换为 bool ,则程序非良构。 |
(C++26 前) |
|
此重载仅当对应的表达式 * lhs @ * rhs 良构且其结果可转换为 bool 时参与重载决议。 |
(C++26 起) |
nullopt
进行比较。等价于与不包含值的
optional
进行比较时的
(1-6)
情形。
|
|
(C++20 起) |
T
的对应运算符)进行值比较。否则,
opt
被视为
小于
value
。
|
若对应表达式 * opt @ value 或 value @ * opt (取决于操作数位置)格式错误或其结果不可转换为 bool ,则程序非良构。 |
(C++26 前) |
|
此重载仅在满足以下所有条件时参与重载决议:
|
(C++26 起) |
目录 |
参数
| lhs, rhs, opt | - |
用于比较的
optional
对象
|
| value | - | 与所含值进行比较的值 |
返回值
( lhs. has_value ( ) == false ? true : * lhs == * rhs )
( lhs. has_value ( ) == false ? false : * lhs ! = * rhs )
异常
注释
| 功能测试 宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_constrained_equality
|
202403L
|
(C++26) | std::optional 的约束比较运算符 |
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 问题报告 | 适用标准 | 已发布行为 | 正确行为 |
|---|---|---|---|
| LWG 2945 | C++17 | 与T类型比较场景的模板参数顺序不一致 | 已统一规范 |