命名空间
变体
操作

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 的相应运算符)。否则,
  • lhs 被认为等于 rhs,当且仅当 lhsrhs 都不包含值时。
  • lhs 被认为小于 rhs,当且仅当 rhs 包含值且 lhs 不包含值时。
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) 当比较抛出异常时,以及抛出什么异常。

[编辑] 注解

特性测试 Std 特性
__cpp_lib_constrained_equality 202403L (C++26) 用于 std::optional 的受约束比较运算符

[编辑] 缺陷报告

以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 2945 C++17 与 T 比较的情况的模板参数顺序不一致 已使其一致