operator==, !=, <, <=, >, >=, <=>(std::optional)
Min standard notice:
Header: <optional>
Performs comparison operations on optional objects.
# Declarations
# Compare two optional objects
template< class T, class U >
constexpr bool operator==( const optional<T>& lhs, const optional<U>& rhs );
(since C++17)
template< class T, class U >
constexpr bool operator!=( const optional<T>& lhs, const optional<U>& rhs );
(since C++17)
template< class T, class U >
constexpr bool operator<( const optional<T>& lhs, const optional<U>& rhs );
(since C++17)
template< class T, class U >
constexpr bool operator<=( const optional<T>& lhs, const optional<U>& rhs );
(since C++17)
template< class T, class U >
constexpr bool operator>( const optional<T>& lhs, const optional<U>& rhs );
(since C++17)
template< class T, class U >
constexpr bool operator>=( const optional<T>& lhs, const optional<U>& rhs );
(since 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 );
(since C++20)
#
Compare an optional object with std::nullopt
template< class T >
constexpr bool operator==( const optional<T>& opt, std::nullopt_t ) noexcept;
(since C++17)
template< class T >
constexpr bool operator==( std::nullopt_t, const optional<T>& opt ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator!=( const optional<T>& opt, std::nullopt_t ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator!=( std::nullopt_t, const optional<T>& opt ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator<( const optional<T>& opt, std::nullopt_t ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator<( std::nullopt_t, const optional<T>& opt ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator<=( const optional<T>& opt, std::nullopt_t ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator<=( std::nullopt_t, const optional<T>& opt ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator>( const optional<T>& opt, std::nullopt_t ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator>( std::nullopt_t, const optional<T>& opt ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator>=( const optional<T>& opt, std::nullopt_t ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr bool operator>=( std::nullopt_t, const optional<T>& opt ) noexcept;
(since C++17) (until C++20)
template< class T >
constexpr std::strong_ordering
operator<=>( const optional<T>& opt, std::nullopt_t ) noexcept;
(since C++20)
# Compare an optional object with a value
template< class T, class U >
constexpr bool operator==( const optional<T>& opt, const U& value );
(since C++17)
template< class U, class T >
constexpr bool operator==( const U& value, const optional<T>& opt );
(since C++17)
template< class T, class U >
constexpr bool operator!=( const optional<T>& opt, const U& value );
(since C++17)
template< class U, class T >
constexpr bool operator!=( const U& value, const optional<T>& opt );
(since C++17)
template< class T, class U >
constexpr bool operator<( const optional<T>& opt, const U& value );
(since C++17)
template< class U, class T >
constexpr bool operator<( const U& value, const optional<T>& opt );
(since C++17)
template< class T, class U >
constexpr bool operator<=( const optional<T>& opt, const U& value );
(since C++17)
template< class U, class T >
constexpr bool operator<=( const U& value, const optional<T>& opt );
(since C++17)
template< class T, class U >
constexpr bool operator>( const optional<T>& opt, const U& value );
(since C++17)
template< class U, class T >
constexpr bool operator>( const U& value, const optional<T>& opt );
(since C++17)
template< class T, class U >
constexpr bool operator>=( const optional<T>& opt, const U& value );
(since C++17)
template< class U, class T >
constexpr bool operator>=( const U& value, const optional<T>& opt );
(since 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 );
(since C++20)
# Parameters
lhs, rhs, opt: an optional object to comparevalue: value to compare to the contained value
# Return value
# Comparing two optionals
lhs == rhs: returnstrueif both optionals contain values and the contained values compare equal, or if both optionals are disengaged. Otherwise returnsfalse.lhs != rhs: returns!(lhs == rhs).lhs < rhs: returnsfalseifrhsis disengaged,trueiflhsis disengaged andrhsis engaged, and otherwise returns*lhs < *rhs.lhs <= rhs: returns!(rhs < lhs).lhs > rhs: returnsrhs < lhs.lhs >= rhs: returns!(lhs < rhs).lhs <=> rhs: if both optionals are engaged, returns*lhs <=> *rhs; otherwise disengaged optionals compare less than engaged optionals, and two disengaged optionals compare equivalent.
#
Comparing an optional with std::nullopt
opt == std::nulloptandstd::nullopt == opt: returntrueifoptis disengaged andfalseotherwise.opt != std::nulloptandstd::nullopt != opt: returntrueifoptcontains a value andfalseotherwise.opt < std::nullopt: always returnsfalse.std::nullopt < opt: returnsopt.has_value().opt <= std::nullopt: returns!opt.has_value().std::nullopt <= opt: always returnstrue.opt > std::nullopt: returnsopt.has_value().std::nullopt > opt: always returnsfalse.opt >= std::nullopt: always returnstrue.std::nullopt >= opt: returns!opt.has_value().opt <=> std::nullopt: returnsstd::strong_ordering::greaterifoptis engaged, otherwisestd::strong_ordering::equal.
# Comparing an optional with a value
opt == valueandvalue == opt: returntrueifoptis engaged and*opt == value; otherwise returnfalse.opt != valueandvalue != opt: return!(opt == value).opt < value: returnstrueifoptis disengaged, otherwise returns*opt < value.value < opt: returnsfalseifoptis disengaged, otherwise returnsvalue < *opt.opt <= value: returnstrueifoptis disengaged, otherwise returns*opt <= value.value <= opt: returnsfalseifoptis disengaged, otherwise returnsvalue <= *opt.opt > value: returnsfalseifoptis disengaged, otherwise returns*opt > value.value > opt: returnstrueifoptis disengaged, otherwise returnsvalue > *opt.opt >= value: returnsfalseifoptis disengaged, otherwise returns*opt >= value.value >= opt: returnstrueifoptis disengaged, otherwise returnsvalue >= *opt.opt <=> value: ifoptis engaged, returns*opt <=> value; otherwise returns a negative comparison result equivalent to disengaged optionals being less than ordinary values.
# Exceptions
- The overloads that compare only against
std::nulloptarenoexceptand do not throw. - The other overloads may throw any exception thrown by the selected comparison operation on the contained values.
- For optional-to-optional comparisons, value comparisons are performed only when both operands are engaged.
- For optional-to-value comparisons, value comparisons are performed only when
optis engaged.
# Notes
# Feature-test macro
| Macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_constrained_equality | 202403L | C++26 | constrained comparison operators for std::optional |
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2945 | C++17 | order of template parameters inconsistent for compare-with-T cases | made consistent |