operator==, !=, <, <=, >, >=, <=>(std::optional)
<optional>
optional
objectsconstexpr bool operator==( const optional<T>& lhs, const optional<U>& rhs );
constexpr bool operator!=( const optional<T>& lhs, const optional<U>& rhs );
constexpr bool operator<( const optional<T>& lhs, const optional<U>& rhs );
constexpr bool operator<=( const optional<T>& lhs, const optional<U>& rhs );
constexpr bool operator>( const optional<T>& lhs, const optional<U>& rhs );
constexpr bool operator>=( const optional<T>& lhs, const optional<U>& rhs );
constexpr std::compare_three_way_result_t <T, U>
optional
object with a nullopt
constexpr bool operator==( const optional<T>& opt, std::nullopt_t ) noexcept;
constexpr bool operator==( std::nullopt_t, const optional<T>& opt ) noexcept;
(until C++20)
constexpr bool operator!=( const optional<T>& opt, std::nullopt_t ) noexcept;
(until C++20)
constexpr bool operator!=( std::nullopt_t, const optional<T>& opt ) noexcept;
(until C++20)
constexpr bool operator<( const optional<T>& opt, std::nullopt_t ) noexcept;
(until C++20)
constexpr bool operator<( std::nullopt_t, const optional<T>& opt ) noexcept;
(until C++20)
constexpr bool operator<=( const optional<T>& opt, std::nullopt_t ) noexcept;
(until C++20)
constexpr bool operator<=( std::nullopt_t, const optional<T>& opt ) noexcept;
(until C++20)
constexpr bool operator>( const optional<T>& opt, std::nullopt_t ) noexcept;
(until C++20)
constexpr bool operator>( std::nullopt_t, const optional<T>& opt ) noexcept;
(until C++20)
constexpr bool operator>=( const optional<T>& opt, std::nullopt_t ) noexcept;
(until C++20)
constexpr bool operator>=( std::nullopt_t, const optional<T>& opt ) noexcept;
(until C++20)
constexpr std::strong_ordering
optional
object with a valueconstexpr bool operator==( const optional<T>& opt, const U& value );
constexpr bool operator==( const U& value, const optional<T>& opt );
constexpr bool operator!=( const optional<T>& opt, const U& value );
constexpr bool operator!=( const U& value, const optional<T>& opt );
constexpr bool operator<( const optional<T>& opt, const U& value );
constexpr bool operator<( const U& value, const optional<T>& opt );
constexpr bool operator<=( const optional<T>& opt, const U& value );
constexpr bool operator<=( const U& value, const optional<T>& opt );
constexpr bool operator>( const optional<T>& opt, const U& value );
constexpr bool operator>( const U& value, const optional<T>& opt );
constexpr bool operator>=( const optional<T>& opt, const U& value );
constexpr bool operator>=( const U& value, const optional<T>& opt );
constexpr std::compare_three_way_result_t <T, U>
Performs comparison operations on optional
objects.
optional
objects, lhs and rhs. The contained values are compared (using the corresponding operator of T
) only if both lhs and rhs contain values. Otherwise,
- lhs is considered equal to rhs if, and only if, both lhs and rhs do not contain a value.
- lhs is considered less than rhs if, and only if, rhs contains a value and lhs does not.
If the corresponding expression *lhs @ *rhs is ill-formed or its result is not convertible to bool, the program is ill-formed.
(until C++26)This overload participates in overload resolution only if the corresponding expression *lhs @ *rhs is well-formed and its result is convertible to bool.
(since C++26)nullopt
. Equivalent to (1-6) when comparing to an optional
that does not contain a value.
The <
, <=
, >
, >=
, and !=
operators are synthesized from operator<=> and operator== respectively.
T
) only if opt contains a value. Otherwise, opt is considered less than value.If the corresponding expression *opt @ value or value @ *opt (depending on the positions of the operands) is ill-formed or its result is not convertible to bool, the program is ill-formed.
(until C++26)This overload participates in overload resolution only if all following conditions are satisfied:
-
U
is not a specialization of std::optional . - The corresponding expression *opt @ value or value @ *opt (depending on the positions of the operands) is well-formed and its result is convertible to bool.
[edit] Parameters
optional
object to compare
[edit] Return value
(lhs.has_value() == false ? true : *lhs == *rhs)
(lhs.has_value() == false ? false : *lhs != *rhs)
[edit] Exceptions
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_constrained_equality |
202403L |
(C++26) | constrained comparison operators for std::optional |
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2945 | C++17 | order of template parameters inconsistent for compare-with-T cases | made consistent |