All pastes #1897896 Raw Edit

Template- Templates

public cpp v1 · immutable
#1897896 ·published 2010-07-10 15:27 UTC
rendered paste body
#include <iostream>using namespace std;template <class T1, template <class T2> class Comparator> struct Pair{  Pair (const T1& p1_, const T1& p2_)  :p1(p1_), p2(p2_)  {  }  const T1& select() const  {    return Comparator<T1>::compare(p1, p2);  }  T1 p1, p2;};template <class T>struct Smaller{  static const T& compare (const T& t1, const T& t2)  {    return t1 < t2 ? t1 : t2;  }};template <class T>struct Greater{  static const T& compare (const T& t1, const T& t2)  {    return t1 > t2 ? t1 : t2;  }};int main(){    Pair<int, Smaller> p1(42,5);    // p1 ist vom Typ T1, also int    // Smaller wird mit p1, also int ausgeprägt    // Pair wird mit int, Smaller ausgeprägt    cout << p1.select() << endl;    Pair<double, Greater> p2(16.0, 22.0);    cout << p2.select() << endl;}