templates/functions/point-norms-and-compare-3/prog.cc - Two Point functions parameterized with a policy class. | Lecture 5 - slide 37 : 40 Program 6 |
// A new example. // In this file we parameterize individual Point functions with a policy. // Compile time parametrization of functions - an alternative to passing function objects at run time. #include <iostream> #include <string> #include "point.h" #include "norms.cc" // distance_to and less_than as template 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") << // Ditto less_than endl << endl; Point p3(1,2), p4(4,11); dist = distance_to<Norm>(p1,p2); // Ditto - here parameterizing with Norm cout << "Distance: " << dist << endl; cout << "Less than? " << (less_than<Norm>(p1,p2) ? "true" : "false") << endl << endl; }