std::rel_ops:: operator!=,>,<=,>=
From cppreference.net
|
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); } } |
Hinweise
Boost.operators
bietet eine vielseitigere Alternative zu
std::rel_ops
.
Seit C++20 sind
std::rel_ops
veraltet zugunsten von
operator<=>
.
Beispiel
Diesen Code ausführen
#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