template <class T> // A template class taking any type T
// This class inherit from std::binary_function
struct greater : binary_function <T, T, bool>
{
// This is a struct (not a class).
// It means members and inheritens is public by default
// This method defines operator() for this class
// you can do: greater<int> op; op(x,y);
bool operator() (const T& x, const T& y) const {
// method is const, this means you can use it
// with a const greater<T> object
return x > y; // use T::operator> const
// if it does not exist, produces a compilation error
}
};
aquí es la definición de std::binary_function
template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
esto le permite acceder a los tipos que define el binary_function
greater<int> op;
greater<int>::result_type res = op(1,2);
lo que equivale a
std::result_of<greater<int>>::type res = op(1,2);
Qué pasa con ella ? Un uso podría ser 'std :: sort (begin (arr), end (arr), std :: greater());' para ordenar un contenedor de enteros de mayor a menor. –
chris