operator==, !=, <, <=, >, >=, <=>(std::optional)
| Defined in header |
||
| Compare two |
||
|
|
(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 |
||
|
|
(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 |
||
|
|
(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.
1-7) Compares two 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,
lhsis considered equal torhsif, and only if, bothlhsandrhsdo not contain a value.lhsis considered less thanrhsif, and only if,rhscontains a value andlhsdoes not.
1-6) Let @ denote the corresponding comparison operator, for each of these functions:
|
If the corresponding expression |
(until C++26) |
|
This overload participates in overload resolution only if the corresponding expression |
(since C++26) |
8-20) Compares opt with a nullopt. Equivalent to (1-6) when comparing to an optional that does not contain a value.
|
The |
(since C++20) |
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.
21-32) Let @ denote the corresponding comparison operator, for each of these functions:
|
If the corresponding expression |
(until C++26) |
|
This overload participates in overload resolution only if all following conditions are satisfied:
|
(since C++26) |
Parameters
| lhs, rhs, opt | - | an optional object to compare
|
| value | - | value to compare to the contained value |
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
Exceptions
1-7) May throw implementation-defined exceptions.
21-33) Throws when and what the comparison throws.
Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_constrained_equality |
202403L |
(C++26) | constrained comparison operators for std::optional |
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 |