On this page
operator==, !=, <, <=, >, >=, <=>(std::optional)
Defined in header <optional> |
||
---|---|---|
Compare two optional objects |
||
|
(1) | (since C++17) |
|
(2) | (since C++17) |
|
(3) | (since C++17) |
|
(4) | (since C++17) |
|
(5) | (since C++17) |
|
(6) | (since C++17) |
|
(7) | (since C++20) |
Compare an optional object with a nullopt |
||
|
(8) | (since C++17) |
|
(9) | (since C++17) (until C++20) |
|
(10) | (since C++17) (until C++20) |
|
(11) | (since C++17) (until C++20) |
|
(12) | (since C++17) (until C++20) |
|
(13) | (since C++17) (until C++20) |
|
(14) | (since C++17) (until C++20) |
|
(15) | (since C++17) (until C++20) |
|
(16) | (since C++17) (until C++20) |
|
(17) | (since C++17) (until C++20) |
|
(18) | (since C++17) (until C++20) |
|
(19) | (since C++17) (until C++20) |
|
(20) | (since C++20) |
Compare an optional object with a value |
||
|
(21) | (since C++17) |
|
(22) | (since C++17) |
|
(23) | (since C++17) |
|
(24) | (since C++17) |
|
(25) | (since C++17) |
|
(26) | (since C++17) |
|
(27) | (since C++17) |
|
(28) | (since C++17) |
|
(29) | (since C++17) |
|
(30) | (since C++17) |
|
(31) | (since C++17) |
|
(32) | (since C++17) |
|
(33) | (since C++20) |
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 torhs
if, and only if, bothlhs
andrhs
do not contain a value.lhs
is considered less thanrhs
if, and only if,rhs
contains a value andlhs
does not.
opt
with a nullopt
. Equivalent to (1-6) when comparing to an optional
that does not contain a value.
The |
(since C++20) |
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
. If the corresponding two-way comparison expression between *opt
and value
is not well-formed, or if its result is not convertible to bool
, the program is ill-formed.
Parameters
lhs, rhs, opt | - | an optional object to compare |
value | - | value to compare to the contained value |
Return value
bool(lhs) != bool(rhs)
, returns false
.Otherwise, if bool(lhs) == false
(and so bool(rhs) == false
as well), returns true
.
Otherwise, returns *lhs == *rhs
.
bool(lhs) != bool(rhs)
, returns true
.Otherwise, if bool(lhs) == false
(and so bool(rhs) == false
as well), returns false
.
Otherwise, returns *lhs != *rhs
.
bool(rhs) == false
returns false
.Otherwise, if bool(lhs) == false
, returns true
.
Otherwise returns *lhs < *rhs
.
bool(lhs) == false
returns true
.Otherwise, if bool(rhs) == false
, returns false
.
Otherwise returns *lhs <= *rhs
.
bool(lhs) == false
returns false
.Otherwise, if bool(rhs) == false
, returns true
.
Otherwise returns *lhs > *rhs
.
bool(rhs) == false
returns true
.Otherwise, if bool(lhs) == false
, returns false
.
Otherwise returns *lhs >= *rhs
.
bool(lhs) && bool(rhs)
is true
returns *x <=> *y
.Otherwise, returns
bool(lhs) <=> bool(rhs)
.
!opt
.
bool(opt)
.
false
.
bool(opt)
.
!opt
.
true
.
bool(opt)
.
false
.
true
.
!opt
.
bool(opt) <=> false
.
bool(opt) ? *opt == value : false
.
bool(opt) ? value == *opt : false
.
bool(opt) ? *opt != value : true
.
bool(opt) ? value != *opt : true
.
bool(opt) ? *opt < value : true
.
bool(opt) ? value < *opt : false
.
bool(opt) ? *opt <= value : true
.
bool(opt) ? value <= *opt : false
.
bool(opt) ? *opt > value : false
.
bool(opt) ? value > *opt : true
.
bool(opt) ? *opt >= value : false
.
bool(opt) ? value >= *opt : true
.
bool(opt) ? *opt <=> value : std::strong_ordering::less
.
Exceptions
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 |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/optional/operator_cmp