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
Release: 2024-12-17 00:57:25
Original
549 people have browsed it

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>;
Copy after login

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>;
Copy after login

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;
};
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template