operator==, !=, <, <=, >, >=, <=> (std::variant)
|
Definiert in Header
<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) |
|
Hilfsfunktionsvorlage
|
||
|
template
<
std::
size_t
I,
class
...
Types
>
constexpr
const
std::
variant_alternative_t
<
I,
std::
variant
<
Types...
>>
&
|
(8) | ( exposition only* ) |
Führt Vergleichsoperationen auf std::variant -Objekten aus.
T
), wenn sowohl
lhs
als auch
rhs
Werte mit demselben Index enthalten. Andernfalls gilt:
- lhs wird als gleich mit rhs betrachtet, genau dann wenn sowohl lhs als auch rhs keinen Wert enthalten.
- lhs wird als kleiner als rhs betrachtet, genau dann wenn entweder rhs einen Wert enthält und lhs nicht, oder lhs. index ( ) kleiner ist als rhs. index ( ) .
|
Falls für einige Werte von
I
der entsprechende Ausdruck
|
(bis C++26) |
|
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, 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
niemals ausgelöst wird.
Inhaltsverzeichnis |
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
#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) |