How to Define a Template Typedef for a Modified Template Class
Consider a template class Matrix with type parameters N and M. To create a "Vector" (column vector) with dimensions equivalent to Matrix
typedef Matrix<N,1> Vector<N>;
However, this approach leads to a compile error.
Alternative and Solution
C 11 introduces alias declarations that generalize typedef and allow templating:
template <size_t N> using Vector = Matrix<N, 1>;
In C 11 and later, the type Vector<3> is equivalent to Matrix<3, 1>.
Workaround for C 03
Prior to C 11, the following is a close approximation:
template <size_t N> struct Vector { typedef Matrix<N, 1> type; };
Here, Vector<3>::type is equivalent to Matrix<3, 1>.
The above is the detailed content of How to Define a Template Typedef for a Modified Template Class in C ?. For more information, please follow other related articles on the PHP Chinese website!