Namespaces
Variants

operator==, !=, <, <=, >, >=, <=>(std::variant)

Aus de.cppreference.net
 
 
Bibliothek für Hilfsprogramme
Sprachunterstützung
Typunterstützung (grundlegende Typen, RTTI)
Bibliotheks-Feature-Test-Makros (C++20)
Programm-Hilfsprogramme
Variadische Funktionen
Koroutinen-Unterstützung (C++20)
Vertragsunterstützung (C++26)
Drei-Wege-Vergleich
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

Allgemeine Hilfsprogramme
Relationale Operatoren (deprecated in C++20)
Ganzzahlvergleichsfunktionen
(C++20)(C++20)(C++20)    
(C++20)
Swap und Typoperationen
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Allgemeine Vokabular-Typen
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
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
  • lhs wird als gleich zu 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 als rhs.index() ist.
1-6) Sei @ den entsprechenden Vergleichsoperator bezeichnen, für jede dieser Funktionen:

Wenn der entsprechende Ausdruck I für bestimmte Werte von GET <I>(lhs) @ GET <I>(rhs) nicht wohlgeformt ist oder sein Ergebnis nicht in bool konvertierbar ist, ist das Programm nicht wohlgeformt.

(bis C++26)

Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn der entsprechende Ausdruck I für alle Werte von GET <I>(lhs) @ GET <I>(rhs) wohlgeformt ist und sein Ergebnis in bool konvertierbar ist.

(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 <=> :

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)