Namespaces
Variants

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

ja.cppreference.net より
 
 
ユーティリティライブラリ
言語サポート
型サポート (基本型、RTTI)
ライブラリ機能テストマクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20 で非推奨)
整数比較関数
(C++20)(C++20)(C++20)    
(C++20)
Swap と 型操作
(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