1.
For a partial specialization of a template, specify only the template parameters, which are actually parameters. Since you already set dim as 2 or 3, there is no need to specify it again.
template<typename T> class Vector<2, T> { ....
2.
Specialization of a class really means changing the whole declaration. Therefore, the s members of the general Vector<dim, T> will not be available in the specialized Vector<2, T> . You can make a general Vector<dim, T> as in an internal base class and create a subclass for specialization only:
template<int dim, typename T> class VectorImpl; ... template<int dim, typename T = float> class Vector : public VectorImpl<dim, T> {}; template<typename T> class Vector<2, T> : public VectorImpl<2, T> { public: T x() const { ... } };
3.
You do not need to define VecType ! Inside the template, you can simply use Vector . A call to the class with the correct parameters will be automatically displayed.
The end result that compiles:
#include <array> template<int dim, typename T> class VectorImpl { public: //typedef Vector<dim, T> VecType; VectorImpl() { } VectorImpl(const VectorImpl& other) { } VectorImpl& operator=(const VectorImpl& other) { return *this; } VectorImpl operator+(const VectorImpl& other) { return *this; } VectorImpl operator-(const VectorImpl& other) { return *this; } T operator*(const VectorImpl& other) { return 0; } protected: std::array<T, dim> elements; }; template <int dim, typename T = float> class Vector : public VectorImpl<dim, T> {}; template<typename T> class Vector<2, T> : public VectorImpl<2, T> { public: T x() const { return this->elements[0]; } T y() const { return this->elements[1]; } }; template<typename T> class Vector<3, T> : public VectorImpl<2, T> { public: T x() const { return this->elements[0]; } T y() const { return this->elements[1]; } T z() const { return this->elements[2]; } }; int main() { Vector<2> v; Vector<3> vv; v + v; vv.z(); }