std::rel_ops:: operator!=,>,<=,>=
From cppreference.net
|
Definido en el encabezado
<utility>
|
||
|
template
<
class
T
>
bool operator ! = ( const T & lhs, const T & rhs ) ; |
(1) | (obsoleto en C++20) |
|
template
<
class
T
>
bool operator > ( const T & lhs, const T & rhs ) ; |
(2) | (obsoleto en C++20) |
|
template
<
class
T
>
bool operator <= ( const T & lhs, const T & rhs ) ; |
(3) | (obsoleto en C++20) |
|
template
<
class
T
>
bool operator >= ( const T & lhs, const T & rhs ) ; |
(4) | (obsoleto en C++20) |
Dado un
operator
==
y un
operator
<
definidos por el usuario para objetos de tipo
T
, implementa la semántica habitual de los demás operadores de comparación.
1)
Implementa
operator
!
=
en términos de
operator
==
.
2)
Implementa
operator
>
en términos de
operator
<
.
3)
Implementa
operator
<=
en términos de
operator
<
.
4)
Implementa
operator
>=
en términos de
operator
<
.
Contenidos |
Parámetros
| lhs | - | argumento izquierdo |
| rhs | - | argumento derecho |
Valor de retorno
1)
Devuelve
true
si
lhs
es
diferente de
rhs
.
2)
Devuelve
true
si
lhs
es
mayor
que
rhs
.
3)
Devuelve
true
si
lhs
es
menor o igual
que
rhs
.
4)
Devuelve
true
si
lhs
es
mayor o igual
que
rhs
.
Implementación posible
(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); } } |
Notas
Boost.operators
proporciona una alternativa más versátil a
std::rel_ops
.
A partir de C++20,
std::rel_ops
están obsoletos en favor de
operator<=>
.
Ejemplo
Ejecutar este código
#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'; }
Salida:
{1} != {2} : true
{1} > {2} : false
{1} <= {2} : true
{1} >= {2} : false