std::rel_ops::operator!=,>,<=,>=
cppreference.com
<tbody>
</tbody>
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