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