Namespaces
Variants

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

From cppreference.net
Utilities library
(Anmerkung: Der bereitgestellte HTML-Code enthält keinen übersetzbaren Text, da alle Tags leer sind. Die Struktur bleibt gemäß den Anforderungen unverändert.)
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)
Hilfsfunktionsvorlage
template < std:: size_t I, class ... Types >

constexpr const std:: variant_alternative_t < I, std:: variant < Types... >> &

GET ( const variant < Types... > & v ) ;
(8) ( exposition only* )

Führt Vergleichsoperationen auf std::variant -Objekten aus.

1-7) Vergleicht zwei std::variant -Objekte lhs und rhs . Die enthaltenen Werte werden nur dann verglichen (unter Verwendung des entsprechenden Operators von 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 ( ) .
1-6) Sei @ der entsprechende Vergleichsoperator, für jede dieser Funktionen:

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

(bis C++26)

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

(seit C++26)
8) Die ausschließlich zur Darstellung dienende Funktionsvorlage GET verhält sich wie std::get (std::variant) , mit der Ausnahme, dass std::bad_variant_access niemals ausgelöst wird.
Wenn I < sizeof... ( Types ) ist false , ist das Programm fehlerhaft.
Wenn I == v. index ( ) false ist, ist das Verhalten undefiniert.

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

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)