next up previous
Next: How do I write Up: What are templates? Previous: How do I specify

How do I write different code for different template parameters?

Use specializations:
template <class T, int N>
class Vector
{
  T	a[N];
public:
  Vector(const T v) { for (unsigned i(0); i < N; ++i) a[i] = v; }
};

template <>
class Vector <double, 3>
{
  double x, y, z;
public:
  Vector(const double v): x(v), y(v), z(v) {}
};



Alexis Angelidis (PhD) 2005-01-11