0

A piece of code for vector operations contains these class template definitions:

template <class T>
class lt { 
    public: 
    static int compare(T a, T b) { return(a < b); } 
};
template <class T>
class gt { 
    public: 
    static int compare(T a, T b) { return(a > b); } 
};

But why? It's not using extra exception handling, and it relies on objects of class T already having operator< and operator>. Is it not just as easy/easier to use the operators? Or should one use template classes for comparisons?

alle_meije
  • 2,424
  • 1
  • 19
  • 40
  • 1
    Without seeing the rest of the code this is used in, hard to say, but I expect it's used so that you can select "sort ascending" and "sort descending", respectively. – Mats Petersson Jul 26 '13 at 09:05
  • Without knowing the full piece of code, and the design decisions behind it, there is really no way of answering this without guessing or having psychic abilities. – Some programmer dude Jul 26 '13 at 09:07
  • There's also the question of why `compare` returns an `int`, rather than a `bool`. In the standard library, the `compare` functions return `int`, but with a value less than, equal to or greater than zero, according to whether the first argument is less than, equal to or greater than the second. – James Kanze Jul 26 '13 at 09:14
  • The file containing the definitions is here [ https://sites.google.com/site/jivsoft/Home/compute-ranks-of-elements-in-a-c---array-or-vector/ranker.h ] -sorting and ranking of numerical vectors. The ascending/descending point makes sense (the comparison function can now be passed as a pointer) but could also have been implemented with operators without loss of genericity? – alle_meije Jul 26 '13 at 09:56

2 Answers2

4

Those templates can be used whenever someone expects a binary predicate, i.e. a free function taking two parameters. An overloaded operator< may not be defined as a free, binary function, so these templates serve as a sort of adapter to let you use existing operators no matter how they were defined, as long as the expression a < b can be resolved.

Note that the standard already provides very similar* templates; they're called std::less and std::greater, and they're used for example by the ordered, associative containers.

*) The standard library predicates provide additional guarantees on how they work on pointers.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

The Thing is that comparison is not always numerical.Sometimes two objects can be compared and greater than or less Than can have new definitions .For example in a class that holds objects of type Coordinates Comparison can be defined in a custom way. For example :

Coordinate c1(3,5)
Coordinate c2(4,2)

We can overload the > operator to return True for c1>c2 whenever c1.x > c2.x or c1.y>c2.y

fer y
  • 505
  • 4
  • 19
  • That explains why you may need to define / overload `operator>` but not what the use of extra class templates would be. – alle_meije Jul 26 '13 at 09:55
  • 1
    @alle_meije The template is a generic way of defining the Class which you intend to run a comparison on. In this Case `Coordinate` is just an example for that. `` represents an arbitrary class. – fer y Jul 26 '13 at 10:07