Namespaces
Variants

operator==, !=, <, <=, >, >=, <=> (std::optional)

From cppreference.net
Utilities library
定义于头文件 <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 >

operator <=> ( const optional < T > & lhs, const optional < U > & rhs ) ;
(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 起)
(

optional 对象执行比较操作。

1-7) 比较两个 optional 对象 lhs rhs 。仅当 lhs rhs 均包含值时,才会对所含值进行比较(使用 T 的对应运算符)。否则:
  • 当且仅当 lhs rhs 均不包含值时,两者被视为 相等
  • 当且仅当 rhs 包含值而 lhs 不包含值时, lhs 被视为 小于 rhs
1-6) @ 表示对应的比较运算符,对于以下每个函数:

若对应的表达式 * lhs @ * rhs 非良构或其结果不可转换为 bool ,则程序非良构。

(C++26 前)

仅当对应的表达式 * lhs @ * rhs 良构且其结果可转换为 bool 时,此重载才参与重载决议。

(C++26 起)
8-20) opt nullopt 进行比较。当与不包含值的 optional 比较时,等同于 (1-6)

< <= > >= != 运算符分别由 operator <=> operator == 合成

(since C++20)
21-33) opt value 进行比较。仅当 opt 包含值时才会(使用 T 的对应运算符)进行值比较。否则,认为 opt 小于 value
21-32) @ 表示对应的比较运算符,对于以下每个函数:

若对应的表达式 * opt @ value value @ * opt (取决于操作数的位置)非良构,或其结果不可转换为 bool ,则程序非良构。

(C++26 前)

仅当满足以下所有条件时,此重载才会参与重载决议:

  • U 不是 std::optional 的特化。
  • 对应的表达式 * opt @ value value @ * opt (取决于操作数的位置)良构且其结果可转换为 bool
(C++26 起)

目录

参数

lhs, rhs, opt - 待比较的 optional 对象
value - 与所含值进行比较的值

返回值

1) lhs. has_value ( ) ! = rhs. has_value ( ) ? false :
( lhs. has_value ( ) == false ? true : * lhs == * rhs )
2) lhs. has_value ( ) ! = rhs. has_value ( ) ? true :
( lhs. has_value ( ) == false ? false : * lhs ! = * rhs )
3) ! rhs ? false : ( ! lhs ? true : * lhs < * rhs )
4) ! lhs ? true : ( ! rhs ? false : * lhs <= * rhs )
5) ! lhs ? false : ( ! rhs ? true : * lhs > * rhs )
6) ! rhs ? true : ( ! lhs ? false : * lhs >= * rhs )
7) lhs && rhs ? * lhs <=> * rhs : lhs. has_value ( ) <=> rhs. has_value ( )
8,9) ! opt
10,11) opt. has_value ( )
12) false
13) opt. has_value ( )
14) ! opt
15) true
16) opt. has_value ( )
17) false
18) true
19) ! opt
20) opt. has_value ( ) <=> false
21) opt. has_value ( ) ? * opt == value : false
22) opt. has_value ( ) ? value == * opt : false
23) opt. has_value ( ) ? * opt ! = value : true
24) opt. has_value ( ) ? value ! = * opt : true
25) opt. has_value ( ) ? * opt < value : true
26) opt. has_value ( ) ? value < * opt : false
27) opt. has_value ( ) ? * opt <= value : true
28) opt. has_value ( ) ? value <= * opt : false
29) opt. has_value ( ) ? * opt > value : false
30) opt. has_value ( ) ? value > * opt : true
31) opt. has_value ( ) ? * opt >= value : false
32) opt. has_value ( ) ? value >= * opt : true
33) opt. has_value ( ) ? * opt <=> value : std :: strong_ordering :: less

异常

1-7) 可能抛出由实现定义的异常。
21-33) 当比较操作抛出异常时,抛出该异常及其内容。

注释

功能测试 标准 特性
__cpp_lib_constrained_equality 202403L (C++26) std::optional 的约束比较运算符

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 2945 C++17 与T类型比较场景的模板参数顺序不一致 已统一顺序