Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'                templates/functions/vers2/cmp.cc - The template function compare used for Point comparison.Lecture 5 - slide 35 : 40
Program 4

// An excerpt of the previously shown program that uses the compare template function.
// Here we show the use of compare on instances of class Point.

#include <iostream>
#include <string>
#include "point.h"

class ComparsionProblem{};

template<typename T> int compare(const T &v1, const T &v2){
  if (v1 < v2) 
    return -1;
  else if (!(v1 < v2) && !(v2 < v1)) 
    return 0;
  else if (v2 < v1)                  
    return 1;
  else 
    throw ComparsionProblem();
}

int main(){
  using namespace std;


  Point p1(1,2), p2(3,4);                   
  cout << compare(p1, p1) << endl;          //  0
  cout << compare(p1, p2) << endl;          // -1
  cout << compare(p2, p1) << endl;          //  1
}