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
Release: 2024-12-30 12:59:11
Original
897 people have browsed it

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

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

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

To find a more suitable solution, we turn to alias declarations introduced in C 11:

template <size_t N>
using Vector = Matrix<N, 1>;
Copy after login

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

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template