std::rel_ops::operator!=,>,<=,>=
Von cppreference.com
| Definiert in der Header-Datei <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 gegebenem benutzerdefinierten operator== und operator< für Objekte des Typs T werden die üblichen Semantiken der anderen Vergleichsoperatoren implementiert.
1) Implementiert operator!= mittels operator==.
2) Implementiert operator> mittels operator<.
3) Implementiert operator<= mittels operator<.
4) Implementiert operator>= mittels operator<.
Inhalt |
[edit] Parameter
| lhs | - | Argument auf der linken Seite |
| rhs | - | Argument auf der rechten Seite |
[edit] Rückgabewert
1) Gibt true zurück, wenn lhs *ungleich* rhs ist.
2) Gibt true zurück, wenn lhs *größer* als rhs ist.
3) Gibt true zurück, wenn lhs *kleiner oder gleich* rhs ist.
4) Gibt true zurück, wenn lhs *größer oder gleich* rhs ist.
[edit] 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); } } |
[edit] Anmerkungen
Boost.operators bietet eine vielseitigere Alternative zu std::rel_ops.
Ab C++20 sind std::rel_ops zugunsten von operator<=> veraltet.
[edit] Beispiel
Führen Sie diesen Code aus
#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