Why is template typedef an issue in C++ (not C++11)

c++, templates, typedef

Solution

The point of `typedef` is to define a type alias. A subclass is not a type alias - it is a new type.

For example, imagine some library function

template<size_t N, size_t M>
Matrix<N, M> * createMatrix();

Now with helper type

Vector<3>::type * var = createMatrix<3, 1>();

is legal. With inheritance

Vector<3> * var = createMatrix<3, 1>();

is not.

Problem

In this question the OP asked for a solution to template typedef which is not possible in C++. The OP also presented a solution themselves but did not like it: ``` template<size_t N, size_t M> class Matrix { // .... }; // Problem - will not compile typedef Matrix<N,1> Vector<N>; // Solution template <int N> class Vector: public Matrix<N,1> { }; ``` My question is, what advantage does the `Helper::type` solution give us over the OP's solution (assuming these classes are never meant to be used by a base pointer or `new`'d as such)? An empty class should carry no overhead in release (or does it?). The only disadvantage I can see is that in debug builds you will have to expand the base class when debugging. EDIT: In addition to the selected answer, see @Dani's answer who suggested that the inherited version would require constructors to be defined, which is an added inconvenience.

Original source

Related problems