std::rel_ops::operator!=,>,<=,>=
Материал из cppreference.com
<tbody>
</tbody>
| Определено в заголовочном файле <utility>
|
||
template< class T > bool operator!=( const T& lhs, const T& rhs ); |
(1) | (устарело в C++20) |
template< class T > bool operator>( const T& lhs, const T& rhs ); |
(2) | (устарело в C++20) |
template< class T > bool operator<=( const T& lhs, const T& rhs ); |
(3) | (устарело в C++20) |
template< class T > bool operator>=( const T& lhs, const T& rhs ); |
(4) | (устарело в C++20) |
При заданных пользователем operator== и operator< для объектов типа T реализует обычную семантику других операторов сравнения.
1) Реализует
operator!= в терминах operator==.2) Реализует
operator> в терминах operator<.3) Реализует
operator<= в терминах operator<.4) Реализует
operator>= в терминах operator<.Параметры
| lhs | — | левый аргумент |
| rhs | — | правый аргумент |
Возвращаемое значение
1) Возвращает
true, если lhs не равно rhs.2) Возвращает
true, если lhs больше rhs.3) Возвращает
true, если lhs меньше или равно rhs.4) Возвращает
true, если lhs больше или равно rhs.Возможная реализация
(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);
}
}
|
Примечание
Boost.operators предоставляет более универсальную альтернативу std::rel_ops.
Начиная с C++20, std::rel_ops устарели в пользу operator<=>.
Пример
Запустить этот код
#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';
}
Вывод:
{1} != {2} : true
{1} > {2} : false
{1} <= {2} : true
{1} >= {2} : false