Re: sorting stl list with predicate
On Jan 28, 6:46 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
ManicQin wrote:
struct greaterScale : public std::greater<PCOperation>
Btw, you don't have to inherit from std::greater (or any
comparator in the STL) in order to write a comparator. This is
template metaprogramming, not object-oriented programming.
True, but providing additional information in the form of
typedefs is sometimes useful. I'd generally inherit from
std::binary_operator< Operation*, Operation*, bool > for
example. Since std::greater< Operation* > inherits from this,
he's effectively done so, with less characters to type, but with
the result of misleading the reader (since his object manifestly
has nothing to do with std::greater).
If you're doing much of this sort of thing, it might be worth
defining a ComparisonOperator class template:
template< typename ArgumentType >
struct ComparisonOperator
: std::binary_operator< ArgumentType, ArgumentType, bool >
{
} ;
and inheriting from it.
(I generally define a compare() member function, then derive
from a ComparisonOperators class template which defines all of
the comparison operators in terms of compare(), and provides the
typedefs.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34