namespace std {
// C++98
template <typename T>
struct greater {
bool operator ()(const T& x, const T& y) const;
using first_argument_type = T;
using second_argument_type = T;
using result_type = bool;
};
// C++14
template <class T = void>
struct greater {
constexpr bool operator()(const T& x, const T& y) const;
using first_argument_type = T;
using second_argument_type = T;
using result_type = bool;
};
template <>
struct greater<void> {
template <class T, class U> auto operator()(T&& t, U&& u) const
-> decltype(std::forward<T>(t) > std::forward<U>(u));
using is_transparent = unspecified;
};
// C++20
template <class T = void>
struct greater {
constexpr bool operator()(const T& x, const T& y) const;
};
template <>
struct greater<void> {
template <class T, class U> auto operator()(T&& t, U&& u) const
-> decltype(std::forward<T>(t) > std::forward<U>(u));
using is_transparent = unspecified;
};
}
概要
greaterクラスは、左辺が右辺より大きいかの比較を行う関数オブジェクトである。
この関数オブジェクトは一切のメンバ変数を持たず、状態を保持しない。
メンバ関数
| 名前 | 説明 |
|---|---|
operator () |
x > y と等価 |
メンバ型
| 名前 | 説明 |
|---|---|
first_argument_type |
operator() の最初の引数の型。T と等価(T が void 以外の場合のみ) |
second_argument_type |
operator() の2番目の引数の型。T と等価(T が void 以外の場合のみ) |
result_type |
operator() の戻り値の型。bool と等価(T が void 以外の場合のみ) |
is_transparent |
operator() が関数テンプレートである事を示すタグ型。実装依存の型であるがあくまでタグ型であり、型そのものには意味はない。( T が void の場合のみ) |
備考
greater<void>のoperator()が組み込みのポインタ比較演算子を呼び出す場合、その比較は厳密な全順序を与える。この順序は、less/greater/less_equal/greater_equalの各特殊化の間で一貫しており、かつ組み込みのポインタ比較演算子が定義される場合はその結果とも一致する。
例
#include <iostream>
#include <functional>
int main()
{
std::cout << std::boolalpha << std::greater<int>()(3, 2) << std::endl;
}
出力
true
参照
- N3421 Making Operator Functors greater<>
- N3657 Adding heterogeneous comparison lookup to associative containers (rev 4)
- N3789 Constexpr Library Additions: functional
- P0005R4 Adopt
not_fnfrom Library Fundamentals 2 for C++17 - P0619R4 Reviewing deprecated facilities of C++17 for C++20
- LWG Issue 2450.
(greater|less|greater_equal|less_equal)<void>do not yield a total order for pointers- C++17で、
greater<void>がポインタを比較する場合に全順序を与えることが規定された(非void版と同様)
- C++17で、
- LWG Issue 2562. Consistent total ordering of pointers by comparison functors
- C++17で、
less/greater/less_equal/greater_equalが同一ポインタ型に対して同じ全順序を与え、組み込みのポインタ比較演算子とも一致することが規定された
- C++17で、