Home >Backend Development >C++ >How to Define a Template Typedef for a Modified Template Class in C ?

How to Define a Template Typedef for a Modified Template Class in C ?

DDD
DDDOriginal
2024-12-17 00:57:25545browse

How to Define a Template Typedef for a Modified Template Class in C  ?

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, a typedef is desired:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn