std::rel_ops:: operator!=,>,<=,>=
From cppreference.net
|
ヘッダーで定義
<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