I am trying to define a Vector
as a row/column Matrix
. Vector
needs some methods which Matrix
doesn't have, so I specialized Matrix
:
template<typename T, unsigned N, unsigned M>
struct Matrix {
T data[N][M];
};
template <typename T, unsigned N>
struct Matrix<T, N, 1> : public Matrix<T, N, 1> {
T at(unsigned index) {
return data[index][0];
}
};
template <typename T, unsigned N>
using Vector = Matrix<T, N, 1>;
This code does not compile, because the specialization is a recursive type. The reason I want inheritance here is so that I can include all of the content of Matrix
into the specialization without copy&paste.
Is there a way that I can instantiate the original Matrix
and inherit from it? And if there was, would my type become non-recursive?
Another way of solving this problem that comes to mind is to simply #include
the contents of a regular Matrix
into both the initial definition and all specializations. But this is far from idiomatic.