std::rel_ops::operator!=,>,<=,>=
Depuis fr.cppreference.net
< cpp | utilitaire
| Défini dans l'en-tête <utility>
|
||
template< class T >
bool operator!=( const T& lhs, const T& rhs );
|
(1) | (déprécié en C++20) |
template< class T >
bool operator>( const T& lhs, const T& rhs );
|
(2) | (déprécié en C++20) |
template< class T >
bool operator<=( const T& lhs, const T& rhs );
|
(3) | (déprécié en C++20) |
template< class T >
bool operator>=( const T& lhs, const T& rhs );
|
(4) | (déprécié en C++20) |
Étant donnés un operator== et un operator< définis par l'utilisateur pour des objets de type T, implémente la sémantique habituelle des autres opérateurs de comparaison.
1) Implémente
operator!= en fonction de operator==.2) Implémente
operator> en fonction de operator<.3) Implémente
operator<= en fonction de operator<.4) Implémente
operator>= en fonction de operator<.Paramètres
| lhs | - | argument de gauche |
| rhs | - | argument de droite |
Valeur de retour
1)
Retourne
true
si
lhs
est
différent de
rhs
.
2)
Retourne
true
si
lhs
est
supérieur
à
rhs
.
3)
Retourne
true
si
lhs
est
inférieur ou égal
à
rhs
.
4)
Retourne
true
si
lhs
est
supérieur ou égal
à
rhs
.
Implémentation possible
(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); } } |
` et `
` n'a pas été traduit
- Les termes spécifiques au C++ (comme `namespace`, `template`, `bool`, etc.) sont conservés en anglais
- La numérotation (1) à (4) est maintenue telle quelle
- La structure du tableau et la mise en forme sont préservées
Notes
Boost.operators
offre une alternative plus polyvalente à
std::rel_ops
.
Depuis C++20,
std::rel_ops
sont dépréciés en faveur de
operator<=>
.
Exemple
Exécuter ce code
#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'; }
Sortie :
{1} != {2} : true
{1} > {2} : false
{1} <= {2} : true
{1} >= {2} : false