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

来自cppreference.com
< cpp‎ | utility‎ | optional


 
 
工具库
语言支持
类型支持(基本类型、RTTI)
库功能特性测试宏 (C++20)
动态内存管理
程序工具
协程支持 (C++20)
变参数函数
调试支持
(C++26)
三路比较
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)(C++20)(C++20)   
(C++20)
交换类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
初等字符串转换
(C++17)
(C++17)

 
 
在标头 <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 起)
(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

    operator<=>( const optional<T>& opt, std::nullopt_t ) noexcept;
(20) (C++20 起)
比较一个 optional 对象与一个值
template< class T, class U >
constexpr bool operator==( const optional<T>& opt, const U& value );
(21) (C++17 起)
template< class T, class U >
constexpr bool operator==( const T& value, const optional<U>& opt );
(22) (C++17 起)
template< class T, class U >
constexpr bool operator!=( const optional<T>& opt, const U& value );
(23) (C++17 起)
template< class T, class U >
constexpr bool operator!=( const T& value, const optional<U>& opt );
(24) (C++17 起)
template< class T, class U >
constexpr bool operator<( const optional<T>& opt, const U& value );
(25) (C++17 起)
template< class T, class U >
constexpr bool operator<( const T& value, const optional<U>& opt );
(26) (C++17 起)
template< class T, class U >
constexpr bool operator<=( const optional<T>& opt, const U& value );
(27) (C++17 起)
template< class T, class U >
constexpr bool operator<=( const T& value, const optional<U>& opt );
(28) (C++17 起)
template< class T, class U >
constexpr bool operator>( const optional<T>& opt, const U& value );
(29) (C++17 起)
template< class T, class U >
constexpr bool operator>( const T& value, const optional<U>& opt );
(30) (C++17 起)
template< class T, class U >
constexpr bool operator>=( const optional<T>& opt, const U& value );
(31) (C++17 起)
template< class T, class U >
constexpr bool operator>=( const T& value, const optional<U>& opt );
(32) (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>& opt, const U& value );
(33) (C++20 起)

进行 optional 对象上的比较。

1-7) 比较两个 optional 对象 lhsrhs。仅若 lhsrhs 都含值,才比较所含值(使用 T 的对应运算符)。否则,
  • 当且仅当 lhsrhs 都不含值时,才认为 lhs 等于 rhs
  • 当且仅当 rhs 含值且 lhs 不含值时,才认为 lhs 小于 rhs
8-20) 比较 optnullopt。当与不含值的 optional 比较时等价于 (1-6)

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

(C++20 起)
21-33) 比较 optvalue。仅当 opt 含值时才比较值。否则认为 opt 小于 value。若 *optvalue 间的对应双路比较表达式非良构,或若结果不能转换为 bool,则程序非良构。

参数

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

返回值

1)bool(lhs) != bool(rhs) 则返回 false

否则,若 bool(lhs) == false(且 bool(rhs) == false 亦然),则返回 true

否则,返回 *lhs == *rhs
2)bool(lhs) != bool(rhs) 则返回 true

否则,若 bool(lhs) == false(且 bool(rhs) == false 亦然),则返回 false

否则,返回 *lhs != *rhs
3)bool(rhs) == false 则返回 false

否则,若 bool(lhs) == false 则返回 true

否则,返回 *lhs < *rhs
4)bool(lhs) == false 则返回 true

否则,若 bool(rhs) == false 则返回 false

否则,返回 *lhs <= *rhs
5)bool(lhs) == false 则返回 false

否则,若 bool(rhs) == false 则返回 true

否则,返回 *lhs > *rhs
6)bool(rhs) == false 则返回 true

否则,若 bool(lhs) == false 则返回 false

否则,返回 *lhs >= *rhs
7)bool(lhs) && bool(rhs)true 则返回 *x <=> *y
否则,返回 bool(lhs) <=> bool(rhs)
8,9) 返回 !opt
10,11) 返回 bool(opt)
12) 返回 false
13) 返回 bool(opt)
14) 返回 !opt
15) 返回 true
16) 返回 bool(opt)
17) 返回 false
18) 返回 true
19) 返回 !opt
20) 返回 bool(opt) <=> false
21) 返回 bool(opt) ? *opt == value : false
22) 返回 bool(opt) ? value == *opt : false
23) 返回 bool(opt) ? *opt != value : true
24) 返回 bool(opt) ? value != *opt : true
25) 返回 bool(opt) ? *opt < value  : true
26) 返回 bool(opt) ? value < *opt  : false
27) 返回 bool(opt) ? *opt <= value : true
28) 返回 bool(opt) ? value <= *opt : false
29) 返回 bool(opt) ? *opt > value  : false
30) 返回 bool(opt) ? value > *opt  : true
31) 返回 bool(opt) ? *opt >= value : false
32) 返回 bool(opt) ? value >= *opt : true
33) 返回 bool(opt) ? *opt <=> value : std::strong_ordering::less

异常

1-7) 可能会抛出由实现定义的异常。
21-33) 在比较抛出时抛出其所抛出的异常。

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 2945 C++17 “与 T 比较”情况的模板形参顺序不一致 使之一致