Namespaces
Variants

std::rel_ops:: operator!=,>,<=,>=

From cppreference.net
Utilities library
General utilities
Relational operators (deprecated in C++20)
rel_ops::operator!= rel_ops::operator>
rel_ops::operator<= rel_ops::operator>=
Integer comparison functions
(C++20) (C++20) (C++20)
(C++20)
Swap and type operations
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)


Definiert im Header <utility>
template < class T >
bool operator ! = ( const T & lhs, const T & rhs ) ;
(1) (veraltet in C++20)
template < class T >
bool operator > ( const T & lhs, const T & rhs ) ;
(2) (veraltet in C++20)
template < class T >
bool operator <= ( const T & lhs, const T & rhs ) ;
(3) (veraltet in C++20)
template < class T >
bool operator >= ( const T & lhs, const T & rhs ) ;
(4) (veraltet in C++20)

Bei einem benutzerdefinierten operator == und operator < für Objekte vom Typ T implementiert die übliche Semantik der anderen Vergleichsoperatoren.

1) Implementiert operator ! = mittels operator == .
2) Implementiert operator > mittels operator < .
3) Implementiert operator <= mittels operator < .
4) Implementiert operator >= mittels operator < .

Inhaltsverzeichnis

Parameter

lhs - linkes Argument
rhs - rechtes Argument

Rückgabewert

1) Gibt true zurück, falls lhs ungleich rhs ist.
2) Gibt true zurück, falls lhs größer als rhs ist.
3) Gibt true zurück, falls lhs kleiner oder gleich rhs ist.
4) Gibt true zurück, falls lhs größer oder gleich rhs ist.

Mögliche Implementierung

(1) operator!=
namespace rel_ops
{
    template<class T>
    bool operator!=(const T& lhs, const T& rhs)
    {
        return !(lhs == rhs);
    }
}
(2) operator>
namespace rel_ops
{
    template<class T>
    bool operator>(const T& lhs, const T& rhs)
    {
        return rhs < lhs;
    }
}
(3) operator<=
namespace rel_ops
{
    template<class T>
    bool operator<=(const T& lhs, const T& rhs)
    {
        return !(rhs < lhs);
    }
}
(4) operator>=
namespace rel_ops
{
    template<class T>
    bool operator>=(const T& lhs, const T& rhs)
    {
        return !(lhs < rhs);
    }
}
**Anmerkung:** Da der Text hauptsächlich aus HTML-Tags, C++-Code und numerischen Markierungen besteht, die nicht übersetzt werden sollten, gibt es hier keinen übersetzbaren Inhalt außer der strukturellen Beschreibung. Die C++-Operatoren und Code-Blöcke bleiben gemäß den Anforderungen unverändert.

Hinweise

Boost.operators bietet eine vielseitigere Alternative zu std::rel_ops .

Seit C++20 sind std::rel_ops veraltet zugunsten von operator<=> .

Beispiel

#include <iostream>
#include <utility>
struct Foo
{
    int n;
};
bool operator==(const Foo& lhs, const Foo& rhs)
{
    return lhs.n == rhs.n;
}
bool operator<(const Foo& lhs, const Foo& rhs)
{
    return lhs.n < rhs.n;
}
int main()
{
    Foo f1 = {1};
    Foo f2 = {2};
    using namespace std::rel_ops;
    std::cout << std::boolalpha
              << "{1} != {2} : " << (f1 != f2) << '\n'
              << "{1} >  {2} : " << (f1 >  f2) << '\n'
              << "{1} <= {2} : " << (f1 <= f2) << '\n'
              << "{1} >= {2} : " << (f1 >= f2) << '\n';
}

Ausgabe:

{1} != {2} : true
{1} >  {2} : false
{1} <= {2} : true
{1} >= {2} : false