Namespaces
Variants

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

From cppreference.net
Utilities library
General utilities
Relational operators (deprecated in C++20)
rel_ops::operator!= rel_ops::operator>
rel_ops::operator<= rel_ops::operator>=
Integer comparison functions
(C++20) (C++20) (C++20)
(C++20)
Swap and type operations
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)


헤더 파일에 정의됨 <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 greater 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