Home >Backend Development >C++ >How Can I Create a Vector Type Alias from a Matrix Template in C ?

How Can I Create a Vector Type Alias from a Matrix Template in C ?

Susan Sarandon
Susan SarandonOriginal
2024-12-30 12:59:11941browse

How Can I Create a Vector Type Alias from a Matrix Template in C  ?

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 that is equivalent to Matrix.

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::type is equivalent to Matrix.

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!

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