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


在标头 <experimental/optional> 定义
Compare two optional objects
template< class T >
constexpr bool operator==( const optional<T>& lhs, const optional<T>& rhs );
(1) (库基础 TS)
template< class T >
constexpr bool operator!=( const optional<T>& lhs, const optional<T>& rhs );
(2) (库基础 TS)
template< class T >
constexpr bool operator<( const optional<T>& lhs, const optional<T>& rhs );
(3) (库基础 TS)
template< class T >
constexpr bool operator<=( const optional<T>& lhs, const optional<T>& rhs );
(4) (库基础 TS)
template< class T >
constexpr bool operator>( const optional<T>& lhs, const optional<T>& rhs );
(5) (库基础 TS)
template< class T >
constexpr bool operator>=( const optional<T>& lhs, const optional<T>& rhs );
(6) (库基础 TS)
Compare an optional object with a nullopt
template< class T >
constexpr bool operator==( const optional<T>& opt, std::nullopt_t ) noexcept;
(7) (库基础 TS)
template< class T >
constexpr bool operator==( std::nullopt_t, const optional<T>& opt ) noexcept;
(8) (库基础 TS)
template< class T >
constexpr bool operator!=( const optional<T>& opt, std::nullopt_t ) noexcept;
(9) (库基础 TS)
template< class T >
constexpr bool operator!=( std::nullopt_t, const optional<T>& opt ) noexcept;
(10) (库基础 TS)
template< class T >
constexpr bool operator<( const optional<T>& opt, std::nullopt_t ) noexcept;
(11) (库基础 TS)
template< class T >
constexpr bool operator<( std::nullopt_t, const optional<T>& opt ) noexcept;
(12) (库基础 TS)
template< class T >
constexpr bool operator<=( const optional<T>& opt, std::nullopt_t ) noexcept;
(13) (库基础 TS)
template< class T >
constexpr bool operator<=( std::nullopt_t, const optional<T>& opt ) noexcept;
(14) (库基础 TS)
template< class T >
constexpr bool operator>( const optional<T>& opt, std::nullopt_t ) noexcept;
(15) (库基础 TS)
template< class T >
constexpr bool operator>( std::nullopt_t, const optional<T>& opt ) noexcept;
(16) (库基础 TS)
template< class T >
constexpr bool operator>=( const optional<T>& opt, std::nullopt_t ) noexcept;
(17) (库基础 TS)
template< class T >
constexpr bool operator>=( std::nullopt_t, const optional<T>& opt ) noexcept;
(18) (库基础 TS)
Compare an optional object with a T
template< class T >
constexpr bool operator==( const optional<T>& opt, const T& value );
(19) (库基础 TS)
template< class T >
constexpr bool operator==( const T& value, const optional<T>& opt );
(20) (库基础 TS)
template< class T >
constexpr bool operator!=( const optional<T>& opt, const T& value );
(21) (库基础 TS)
template< class T >
constexpr bool operator!=( const T& value, const optional<T>& opt );
(22) (库基础 TS)
template< class T >
constexpr bool operator<( const optional<T>& opt, const T& value );
(23) (库基础 TS)
template< class T >
constexpr bool operator<( const T& value, const optional<T>& opt );
(24) (库基础 TS)
template< class T >
constexpr bool operator<=( const optional<T>& opt, const T& value );
(25) (库基础 TS)
template< class T >
constexpr bool operator<=( const T& value, const optional<T>& opt );
(26) (库基础 TS)
template< class T >
constexpr bool operator>( const optional<T>& opt, const T& value );
(27) (库基础 TS)
template< class T >
constexpr bool operator>( const T& value, const optional<T>& opt );
(28) (库基础 TS)
template< class T >
constexpr bool operator>=( const optional<T>& opt, const T& value );
(29) (库基础 TS)
template< class T >
constexpr bool operator>=( const T& value, const optional<T>& opt );
(30) (库基础 TS)

optional 对象上实施比较操作。

1-6) 比较两个 optional 对象 lhsrhs。仅当 lhsrhs 都含有值时,比较所含值((1,2) 使用 operator==(3-6) 使用 operator<)。否则,
  • 当且仅当 lhsrhs 均不含有值时,lhs 被认为等于 rhs
  • 当且仅当 rhs 含有值而 lhs 不含值时,lhs 被认为小于 rhs
7-18) 比较 optnullopt。等价于 (1-6) 与不含值的 optional 比较。
19-30) 比较 optvalue。仅当 opt 含有值时,比较两个值((19-22) 使用 operator==(23-30) 使用 operator<)。否则,opt 被当做小于 value

参数

lhs, rhs, opt - 要比较的 optional 对象
value - 要愚所含值比较的值
类型要求
-
为使用重载 (1,2), T 必须满足可相等比较 (EqualityComparable)

返回值

1) 如果 bool(lhs) != bool(rhs) 则返回 false
否则,如果 bool(lhs) == false(而且 bool(rhs) == false)则返回 true
否则返回 *lhs == *rhs
2) 返回 !(lhs == rhs)
3) 如果 bool(rhs) == false 则返回 false
否则,如果 bool(lhs) == false,则返回 true
否则返回 *x < *y
4) 返回 !(rhs < lhs)
5) 返回 rhs < lhs
6) 返回 !(lhs < rhs)
7,8) 返回 !opt
9,10) 返回 bool(opt)
11) 返回 false
12) 返回 bool(opt)
13) 返回 !opt
14) 返回 true
15) 返回 bool(opt)
16) 返回 false
17) 返回 true
18) 返回 !opt
19) 返回 bool(opt) ? *opt == value : false
20) 返回 bool(opt) ? value == *opt : false
21) 返回 bool(opt) ? !(*opt == value) : true
22) 返回 bool(opt) ? !(value == *opt) : true
23) 返回 bool(opt) ? *opt < value : true
24) 返回 bool(opt) ? value < *opt : false
25) 返回 !(opt > value)
26) 返回 !(value > opt)
27) 返回 bool(opt) ? value < *opt : false
28) 返回 bool(opt) ? *opt < value : true
29) 返回 !(opt < value)
30) 返回 !(value < opt)

异常

1-6) (无)
19-30) (无)