命名空间
变体
操作

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

来自 cppreference.cn
< 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 包含值时,才比较它们所包含的值(使用 T 对应的运算符)。否则,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 与 T 比较的模板参数顺序不一致 已使其保持一致