| A sample application - together with policy parameterized functions. | Lecture 5 - slide 9 : 39 Program 8 |
// Parameterizing individual functions with a policy.
#include <iostream>
#include <string>
#include "point.h"
#include "norms.cc"
// Generic distance_to and less_than functions:
template<typename N> double distance_to(const Point& p, const Point& q){
return N::distance_to(p,q);
}
template<typename N> bool less_than(const Point& p, const Point& q){
return N::less_than(p,q);
}
int main(){
using namespace std;
double dist;
Point p1(1,2),
p2(4,11);
dist = distance_to<HorizontalNorm>(p1,p2); // Parameterizing distance_to with a
cout << "Distance: " << dist << endl; // ... bundle of two functions from HorizontalNorm
cout << "Less than? " << (less_than<HorizontalNorm>(p1,p2) ? "true" : "false") << endl << endl; // Ditto less_than
Point p3(1,2),
p4(4,11);
dist = distance_to<Norm>(p1,p2);
cout << "Distance: " << dist << endl;
cout << "Less than? " << (less_than<Norm>(p1,p2) ? "true" : "false") << endl << endl;
}