operator==, !=, <, <=, >, >=, <=>(std::variant)
Defined in header <variant>
|
||
template< class... Types > constexpr bool operator==( const std::variant<Types...>& lhs, |
(1) | (since C++17) |
template< class... Types > constexpr bool operator!=( const std::variant<Types...>& lhs, |
(2) | (since C++17) |
template< class... Types > constexpr bool operator<( const std::variant<Types...>& lhs, |
(3) | (since C++17) |
template< class... Types > constexpr bool operator>( const std::variant<Types...>& lhs, |
(4) | (since C++17) |
template< class... Types > constexpr bool operator<=( const std::variant<Types...>& lhs, |
(5) | (since C++17) |
template< class... Types > constexpr bool operator>=( const std::variant<Types...>& lhs, |
(6) | (since C++17) |
template< class... Types > constexpr std::common_comparison_category_t |
(7) | (since C++20) |
Helper function template |
||
template< std::size_t I, class... Types > constexpr const std::variant_alternative_t<I, std::variant<Types...>>& |
(8) | (exposition only*) |
Performs comparison operations on std::variant objects.
T
) only if both lhs and rhs contain values corresponding to the same index. 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, either rhs contains a value and lhs does not, or lhs.index() is less than rhs.index().
If, for some values of I, the corresponding expression |
(until C++26) |
This overload participates in overload resolution only if for all values of I, the corresponding expression |
(since C++26) |
GET
behaves like std::get(std::variant)
, except that std::bad_variant_access is never thrown.Contents |
[edit] Parameters
lhs,rhs | - | variants to compare |
[edit] Return value
Operator | Both operands contains a value (let I be lhs.index() and J be rhs.index()) |
lhs or rhs is valueless (let lhs_empty be lhs.valueless_by_exception() and rhs_empty be rhs.valueless_by_exception()) | |
---|---|---|---|
I and J are equal | I and J are unequal | ||
== | GET <I>(lhs) == GET <I>(rhs)
|
false | lhs_empty && rhs_empty |
!= | GET <I>(lhs) != GET <I>(rhs)
|
true | lhs_empty != rhs_empty |
< | GET <I>(lhs) < GET <I>(rhs)
|
lhs.index() < rhs.index() | lhs_empty && !rhs_empty |
> | GET <I>(lhs) > GET <I>(rhs)
|
lhs.index() > rhs.index() | !lhs_empty && rhs_empty |
<= | GET <I>(lhs) <= GET <I>(rhs)
|
lhs.index() < rhs.index() | lhs_empty |
>= | GET <I>(lhs) >= GET <I>(rhs)
|
lhs.index() > rhs.index() | rhs_empty |
<=> | GET <I>(lhs) <=> GET <I>(rhs)
|
lhs.index() <=> rhs.index() | see below |
For operator<=>:
- If only lhs is valueless, returns std::strong_ordering::less.
- If only rhs is valueless, returns std::strong_ordering::greater.
- If both lhs and rhs are valueless, returns std::strong_ordering::equal.
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_constrained_equality |
202403L |
(C++26) | constrained comparison operators for std::variant |
[edit] Example
#include <iostream> #include <string> #include <variant> int main() { std::cout << std::boolalpha; std::string cmp; bool result; auto print2 = [&cmp, &result](const auto& lhs, const auto& rhs) { std::cout << lhs << ' ' << cmp << ' ' << rhs << " : " << result << '\n'; };