operator==, !=, <, <=, >, >=, <=>(std::variant)
Aus de.cppreference.net
| Definiert in Header <variant>
|
||
template< class... Types >
constexpr bool operator==( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(1) | (seit C++17) |
template< class... Types >
constexpr bool operator!=( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(2) | (seit C++17) |
template< class... Types >
constexpr bool operator<( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(3) | (seit C++17) |
template< class... Types >
constexpr bool operator>( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(4) | (seit C++17) |
template< class... Types >
constexpr bool operator<=( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(5) | (seit C++17) |
template< class... Types >
constexpr bool operator>=( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(6) | (seit C++17) |
template< class... Types >
constexpr std::common_comparison_category_t
<std::compare_three_way_result_t<Types>...>
operator<=>( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(7) | (seit C++20) |
| Hilfsfunktions-Schablone |
||
template< std::size_t I, class... Types >
constexpr const std::variant_alternative_t<I, std::variant<Types...>>&
GET( const variant<Types...>& v );
|
(8) | (nur zur Darstellung*) |
Führt Vergleichsoperationen auf std::variant-Objekten aus.
1-7) Vergleicht zwei std::variant-Objekte
lhs und rhs. Die enthaltenen Werte werden (unter Verwendung des entsprechenden Operators von T) nur dann verglichen, wenn sowohl lhs als auch rhs Werte enthalten, die demselben Index entsprechen. Andernfalls
lhswird als gleich zurhsbetrachtet, genau dann, wenn sowohllhsals auchrhskeinen Wert enthalten.lhswird als kleiner alsrhsbetrachtet, genau dann, wenn entwederrhseinen Wert enthält undlhsnicht, oderlhs.index()kleiner alsrhs.index()ist.
1-6) Sei
@ den entsprechenden Vergleichsoperator bezeichnen, für jede dieser Funktionen:
|
Wenn der entsprechende Ausdruck |
(bis C++26) |
|
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn der entsprechende Ausdruck |
(seit C++26) |
8) Die nur zur Darstellung dienende Funktionsschablone
GET verhält sich wie std::get(std::variant), außer dass std::bad_variant_access niemals geworfen wird. Wenn
I < sizeof...(Types) false ist, ist das Programm nicht wohlgeformt. Wenn
I == v.index() false ist, ist das Verhalten undefiniert.Parameter
| lhs,rhs | - | zu vergleichende Varianten |
Rückgabewert
| Operator |
Beide Operanden enthalten einen Wert
(sei I gleich lhs. index ( ) und J gleich rhs. index ( ) ) |
lhs
oder
rhs
ist wertlos
(sei lhs_empty gleich lhs. valueless_by_exception ( ) und rhs_empty gleich 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, wird std::strong_ordering::less zurückgegeben.
- Wenn nur rhs wertlos ist, wird std::strong_ordering::greater zurückgegeben.
- Wenn sowohl lhs als auch rhs wertlos sind, wird std::strong_ordering::equal zurückgegeben.
Hinweise
| Feature-Test Makro | Wert | Std | Funktion |
|---|---|---|---|
__cpp_lib_constrained_equality
|
202403L
|
(C++26) | eingeschränkte Vergleichsoperatoren für std::variant |
Beispiel
Diesen Code ausführen
#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 = "=="; // standardmäßig 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; // Kompilierungsfehler: keine bekannte Konvertierung } // TODO: C++20 Drei-Wege-Vergleichsoperator <=> für Varianten }
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
Siehe auch
|
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
|
vergleicht
optional
-Objekte
(Funktions-Template) |