operator==, !=, <, <=, >, >=, <=>(std::optional)_C++中文网

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 对象与一个 T

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 比较。与 (1-6) 中与不含值的 optional 比较时等价。

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++ 标准。

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