Compare two optional
objects
template< class T, class U >
constexpr bool operator==( const optional<T>& lhs, const optional<U>& rhs );
template< class T, class U >
constexpr bool operator!=( const optional<T>& lhs, const optional<U>& rhs );
template< class T, class U >
constexpr bool operator<( const optional<T>& lhs, const optional<U>& rhs );
template< class T, class U >
constexpr bool operator<=( const optional<T>& lhs, const optional<U>& rhs );
template< class T, class U >
constexpr bool operator>( const optional<T>& lhs, const optional<U>& rhs );
template< class T, class U >
constexpr bool operator>=( const optional<T>& lhs, const optional<U>& rhs );
Compare an optional
object with a nullopt
Compare an optional
object with a value
template< class T, class U >
constexpr bool operator==( const optional<T>& opt, const U& value );
template< class U, class T >
constexpr bool operator==( const U& value, const optional<T>& opt );
template< class T, class U >
constexpr bool operator!=( const optional<T>& opt, const U& value );
template< class U, class T >
constexpr bool operator!=( const U& value, const optional<T>& opt );
template< class T, class U >
constexpr bool operator<( const optional<T>& opt, const U& value );
template< class U, class T >
constexpr bool operator<( const U& value, const optional<T>& opt );
template< class T, class U >
constexpr bool operator<=( const optional<T>& opt, const U& value );
template< class U, class T >
constexpr bool operator<=( const U& value, const optional<T>& opt );
template< class T, class U >
constexpr bool operator>( const optional<T>& opt, const U& value );
template< class U, class T >
constexpr bool operator>( const U& value, const optional<T>& opt );
template< class T, class U >
constexpr bool operator>=( const optional<T>& opt, const U& value );
template< class U, class T >
constexpr bool operator>=( const U& value, const optional<T>& opt );
Performs comparison operations on optional
objects.
Compares two
optional
objects,
lhsand
rhs. The contained values are compared (using the corresponding operator of
T
) only if both
lhsand
rhscontain values. Otherwise,
Let
@denote the corresponding comparison operator, for each of these functions:
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) 8-20)Compares
optwith a
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.
21-33) Compares opt with a value. The values are compared (using the corresponding operator of T
) only if opt contains a value. Otherwise, opt is considered less than value.
Let
@denote the corresponding comparison operator, for each of these functions:
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.optional
object to compare value - value to compare to the contained value [edit] Return 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
[edit] Exceptions1-7) May throw implementation-defined exceptions.
21-33) Throws when and what the comparison throws.
[edit] Notes [edit] Defect reportsThe 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 consistentRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4