operator==, !=, <, <=, >, >=, <=>(std::optional)
来自cppreference.com
在标头 <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 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> |
(33) | (C++20 起) |
进行 optional
对象上的比较。
1-7) 比较两个
optional
对象 lhs 和 rhs。仅若 lhs 与 rhs 都含值,才比较所含值(使用 T
的对应运算符)。否则, - 当且仅当 lhs 与 rhs 都不含值时,才认为 lhs 等于 rhs。
- 当且仅当 rhs 含值且 lhs 不含值时,才认为 lhs 小于 rhs。
8-20) 比较 opt 与
nullopt
。当与不含值的 optional
比较时等价于 (1-6)。
|
(C++20 起) |
21-33) 比较 opt 与 value。仅当 opt 含值时才比较值。否则认为 opt 小于 value。若 *opt 与 value 间的对应双路比较表达式非良构,或若结果不能转换为 bool,则程序非良构。
参数
lhs, rhs, opt | - | 要比较的 optional 对象
|
value | - | 与所含值比较的值 |
返回值
1) 若 bool(lhs) != bool(rhs) 则返回 false。
否则,若 bool(lhs) == false(且 bool(rhs) == false 亦然),则返回 true。
2) 若 bool(lhs) != bool(rhs) 则返回 true。
否则,若 bool(lhs) == false(且 bool(rhs) == false 亦然),则返回 false。
3) 若 bool(rhs) == false 则返回 false。
否则,若 bool(lhs) == false 则返回 true。
4) 若 bool(lhs) == false 则返回 true。
否则,若 bool(rhs) == false 则返回 false。
5) 若 bool(lhs) == false 则返回 false。
否则,若 bool(rhs) == false 则返回 true。
6) 若 bool(rhs) == false 则返回 true。
否则,若 bool(lhs) == false 则返回 false。
7) 若 bool(lhs) && bool(rhs) 为 true 则返回 *x <=> *y。
否则,返回 bool(lhs) <=> bool(rhs)。
否则,返回 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 比较”情况的模板形参顺序不一致 | 使之一致 |