operator==, !=, <, <=, >, >=, <=>(std::variant)
| Definiert in der Header-Datei <variant> |
||
| template< class... Types > constexpr bool operator==( const std::variant<Types...>& lhs, |
(1) | (seit C++17) |
| template< class... Types > constexpr bool operator!=( const std::variant<Types...>& lhs, |
(2) | (seit C++17) |
| template< class... Types > constexpr bool operator<( const std::variant<Types...>& lhs, |
(3) | (seit C++17) |
| template< class... Types > constexpr bool operator>( const std::variant<Types...>& lhs, |
(4) | (seit C++17) |
| template< class... Types > constexpr bool operator<=( const std::variant<Types...>& lhs, |
(5) | (seit C++17) |
| template< class... Types > constexpr bool operator>=( const std::variant<Types...>& lhs, |
(6) | (seit C++17) |
| template< class... Types > constexpr std::common_comparison_category_t |
(7) | (seit C++20) |
| Helper function template |
||
template< std::size_t I, class... Types > constexpr const std::variant_alternative_t<I, std::variant<Types...>>& |
(8) | (nur Exposition*) |
Führt Vergleichsoperationen für std::variant-Objekte durch.
T), nur wenn sowohl lhs als auch rhs Werte enthalten, die dem gleichen Index entsprechen. Andernfalls,- lhs ist *gleich* rhs genau dann, wenn weder lhs noch rhs einen Wert enthalten.
- lhs wird als *kleiner als* rhs betrachtet, wenn und nur wenn entweder rhs einen Wert enthält und lhs nicht, oder lhs.index() kleiner ist als rhs.index().
|
Wenn für einige Werte von I der entsprechende Ausdruck |
(bis C++26) |
|
Diese Überladung nimmt an der Überladungsauflösung teil, nur wenn für alle Werte von I der entsprechende Ausdruck |
(seit C++26) |
GET verhält sich wie std::get(std::variant), mit der Ausnahme, dass std::bad_variant_access nie geworfen wird.Inhalt |
[edit] Parameter
| lhs,rhs | - | Zu vergleichende variants |
[edit] Rückgabewert
| Operator | Beide Operanden enthalten einen Wert (sei I lhs.index() und J rhs.index()) |
lhs oder rhs ist wertlos (sei lhs_empty lhs.valueless_by_exception() und rhs_empty rhs.valueless_by_exception()) | |
|---|---|---|---|
| I und J sind gleich | I und J sind ungleich | ||
| == | 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() | siehe unten |
Für operator<=>
- Wenn nur lhs wertlos ist, gibt std::strong_ordering::less zurück.
- Wenn nur rhs wertlos ist, gibt std::strong_ordering::greater zurück.
- Wenn sowohl lhs als auch rhs wertlos sind, gibt std::strong_ordering::equal zurück.
[edit] Anmerkungen
| Feature-Test-Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_constrained_equality |
202403L |
(C++26) | eingeschränkte Vergleichsoperatoren für std::variant |
[edit] Beispiel
#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'; }; std::variant<int, std::string> v1, v2; std::cout << "operator==\n"; { cmp = "=="; // by default v1 = 0, v2 = 0; result = v1 == v2; // true std::visit(print2, v1, v2); v1 = v2 = 1; result = v1 == v2; // true std::visit(print2, v1, v2); v2 = 2; result = v1 == v2; // false std::visit(print2, v1, v2); v1 = "A"; result = v1 == v2; // false: v1.index == 1, v2.index == 0 std::visit(print2, v1, v2); v2 = "B"; result = v1 == v2; // false std::visit(print2, v1, v2); v2 = "A"; result = v1 == v2; // true std::visit(print2, v1, v2); } std::cout << "operator<\n"; { cmp = "<"; v1 = v2 = 1; result = v1 < v2; // false std::visit(print2, v1, v2); v2 = 2; result = v1 < v2; // true std::visit(print2, v1, v2); v1 = 3; result = v1 < v2; // false std::visit(print2, v1, v2); v1 = "A"; v2 = 1; result = v1 < v2; // false: v1.index == 1, v2.index == 0 std::visit(print2, v1, v2); v1 = 1; v2 = "A"; result = v1 < v2; // true: v1.index == 0, v2.index == 1 std::visit(print2, v1, v2); v1 = v2 = "A"; result = v1 < v2; // false std::visit(print2, v1, v2); v2 = "B"; result = v1 < v2; // true std::visit(print2, v1, v2); v1 = "C"; result = v1 < v2; // false std::visit(print2, v1, v2); } { std::variant<int, std::string> v1; std::variant<std::string, int> v2; // v1 == v2; // Compilation error: no known conversion } // TODO: C++20 three-way comparison operator <=> for variants }
Ausgabe
operator== 0 == 0 : true 1 == 1 : true 1 == 2 : false A == 2 : false A == B : false A == A : true operator< 1 < 1 : false 1 < 2 : true 3 < 2 : false A < 1 : false 1 < A : true A < A : false A < B : true C < B : false
[edit] Siehe auch
| (C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20) |
vergleicht optional-Objekte(function template) |