C Template Typedef for Matrix Column Vector
In C , a common task is to create a column vector equivalent to a matrix with specific dimensions. For instance, a Vector class derived from a Matrix class would be desirable. Unfortunately, using the standard typedef mechanism for this purpose leads to compilation errors.
C 11 Solution: Alias Declarations
C 11 introduced alias declarations, a generalization of typedef, which allows for template specialization. The following code provides a solution:
template <size_t N> using Vector = Matrix<N, 1>;
With this declaration, the type Vector<3> is equivalent to Matrix<3, 1>.
C 03 Workaround
In C 03, the workaround most similar to the alias declaration is to use a nested typedef:
template <size_t N> struct Vector { typedef Matrix<N, 1> type; };
In this case, Vector<3>::type is equivalent to Matrix<3, 1>.
The above is the detailed content of How Can I Define a Column Vector Type Using C Templates?. For more information, please follow other related articles on the PHP Chinese website!