std::rel_ops::operator!=,>,<=,>=
Определено в заголовке <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
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/utility/rel_ops/operator_cmp