std::rel_ops::operator!=,>,<=,>=

From cppreference.com
< cpp‎ | utility
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
rel_ops::operator!=rel_ops::operator>
   
rel_ops::operator<=rel_ops::operator>=
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
Defined in header <utility>
template< class T >
bool operator!=( const T& lhs, const T& rhs );
(1) (deprecated in 哋它亢++20)
template< class T >
bool operator>( const T& lhs, const T& rhs );
(2) (deprecated in 哋它亢++20)
template< class T >
bool operator<=( const T& lhs, const T& rhs );
(3) (deprecated in 哋它亢++20)
template< class T >
bool operator>=( const T& lhs, const T& rhs );
(4) (deprecated in 哋它亢++20)

Given a user-defined operator== and operator< for objects of type T, implements the usual semantics of other comparison operators.

1) Implements operator!= in terms of operator==.
2) Implements operator> in terms of operator<.
3) Implements operator<= in terms of operator<.
4) Implements operator>= in terms of operator<.

Parameters

lhs - left-hand argument
rhs - right-hand argument

Return value

1) Returns true if lhs is not equal to rhs.
2) Returns true if lhs is greater than rhs.
3) Returns true if lhs is less or equal to rhs.
4) Returns true if lhs is greater or equal to rhs.

Possible implementation

(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);
    }
}

Notes

Boost.operators provides a more versatile alternative to std::rel_ops.

As of 哋它亢++20, std::rel_ops are deprecated in favor of operator<=>.

Example

#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';
}

Output:

{1} != {2} : true
{1} >  {2} : false
{1} <= {2} : true
{1} >= {2} : false