Namespaces
Variants

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

cppreference.net에서 온 문서
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐기됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
스왑 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
헤더 <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