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

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


 
 
 
 
在标头 <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 U, class T >
constexpr bool operator==( const U& value, const optional<T>& opt );
(22) (C++17 起)
template< class T, class U >
constexpr bool operator!=( const optional<T>& opt, const U& value );
(23) (C++17 起)
template< class U, class T >
constexpr bool operator!=( const U& value, const optional<T>& opt );
(24) (C++17 起)
template< class T, class U >
constexpr bool operator<( const optional<T>& opt, const U& value );
(25) (C++17 起)
template< class U, class T >
constexpr bool operator<( const U& value, const optional<T>& opt );
(26) (C++17 起)
template< class T, class U >
constexpr bool operator<=( const optional<T>& opt, const U& value );
(27) (C++17 起)
template< class U, class T >
constexpr bool operator<=( const U& value, const optional<T>& opt );
(28) (C++17 起)
template< class T, class U >
constexpr bool operator>( const optional<T>& opt, const U& value );
(29) (C++17 起)
template< class U, class T >
constexpr bool operator>( const U& value, const optional<T>& opt );
(30) (C++17 起)
template< class T, class U >
constexpr bool operator>=( const optional<T>& opt, const U& value );
(31) (C++17 起)
template< class U, class T >
constexpr bool operator>=( const U& value, const optional<T>& 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
1-6)@ 表示对应的比较运算符,对于每个函数:

如果对应的表达式 *lhs @ *rhs 非良构或它的结果不可转换到 bool,那么程序非良构。

(C++26 前)

此重载只有在表达式 *lhs @ *rhs 良构且它的结果可转换到 bool 时才会参与重载决议。

(C++26 起)
8-20) 比较 optnullopt。当与不含值的 optional 比较时等价于 (1-6)

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

(C++20 起)
21-33) 比较 optvalue。只有在 opt 含值时才会比较值。否则认为 opt 小于 value
21-32)@ 表示对应的比较运算符,对于每个函数:

如果对应的表达式 *opt @ valuevalue @ *opt(取决于操作数的位置)非良构或它的结果不可转换到 bool,那么程序非良构。

(C++26 前)

此重载只有在满足以下所有条件时才会参与重载决议:

  • U 不是 std::optional 的特化。
  • 对应的表达式 *opt @ valuevalue @ *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 “与值比较”情况的模板形参顺序不一致 使之一致