Namespaces
Variants
Views
Actions

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

From natekohl.net
< cpp‎ | utility
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Library feature-test macros
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
rel_ops::operator!=rel_ops::operator>rel_ops::operator<=rel_ops::operator>=
Comparisons (C++20)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Swap, forward and move
(C++14)
(C++11)
(C++11)
Elementary string conversions
(C++17)
(C++17)
Type operations
(C++11)
(C++17)
(C++17)
 
Defined in header <utility>
template< class T >
bool operator!=( const T& lhs, const T& rhs );
(1) (deprecated in C++20)
template< class T >
bool operator>( const T& lhs, const T& rhs );
(2) (deprecated in C++20)
template< class T >
bool operator<=( const T& lhs, const T& rhs );
(3) (deprecated in C++20)
template< class T >
bool operator>=( const T& lhs, const T& rhs );
(4) (deprecated in C++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<.

Contents

[edit] Parameters

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

[edit] 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.

[edit] Possible implementation

First version
namespace rel_ops {
    template< class T >
    bool operator!=( const T& lhs, const T& rhs ) 
    {
        return !(lhs == rhs);
    }
}
Second version
namespace rel_ops {
    template< class T >
    bool operator>( const T& lhs, const T& rhs ) 
    {
        return rhs < lhs;
    }
}
Third version
namespace rel_ops {
    template< class T >
    bool operator<=( const T& lhs, const T& rhs ) 
    {
        return !(rhs < lhs);
    }
}
Fourth version
namespace rel_ops {
    template< class T >
    bool operator>=( const T& lhs, const T& rhs ) 
    {
        return !(lhs < rhs);
    }
}

[edit] Notes

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

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

[edit] 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;
    std::cout << "not equal?     : " << (f1 != f2) << '\n';
    std::cout << "greater?       : " << (f1 > f2) << '\n';
    std::cout << "less equal?    : " << (f1 <= f2) << '\n';
    std::cout << "greater equal? : " << (f1 >= f2) << '\n';
}

Output:

not equal?     : true
greater?       : false
less equal?    : true
greater equal? : false