Creating a Typedef for a Matrix-like Vector Using Template Aliases
Consider the following class template:
template<size_t N, size_t M> class Matrix { // .... };
The goal is to define a typedef which creates a Vector (column vector) that is equivalent to a Matrix with sizes N and 1. Initially, the attempt was made using a typedef:
typedef Matrix<N,1> Vector<N>;
However, this resulted in a compile error. A similar but not identical solution was achieved using class inheritance:
template <size_t N> class Vector: public Matrix<N,1> { };
To find a more suitable solution, we turn to alias declarations introduced in C 11:
template <size_t N> using Vector = Matrix<N, 1>;
This allows for the creation of a type alias Vector
In C 03, a similar approximation can be achieved using a struct with a nested typedef:
template <size_t N> struct Vector { typedef Matrix<N, 1> type; };
Here, the type Vector
The above is the detailed content of How Can I Create a Vector Type Alias from a Matrix Template in C ?. For more information, please follow other related articles on the PHP Chinese website!