HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

C++ template typedef

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
typedeftemplatestackoverflow

Problem

I have a class

template
class Matrix {
    // ....
};


I want to make a typedef which creates a Vector (column vector) which is equivalent to a Matrix with sizes N and 1. Something like that:

typedef Matrix Vector;


Which produces compile error. The following creates something similar, but not exactly what I want:

template 
class Vector: public Matrix
{ };


Is there a solution or a not too expensive workaround / best-practice for it?

Solution

C++11 added alias declarations, which are generalization of typedef, allowing templates:

template 
using Vector = Matrix;


The type Vector is equivalent to Matrix.

In C++03, the closest approximation was:

template 
struct Vector
{
    typedef Matrix type;
};


Here, the type Vector::type is equivalent to Matrix.

Code Snippets

template <size_t N>
using Vector = Matrix<N, 1>;
template <size_t N>
struct Vector
{
    typedef Matrix<N, 1> type;
};

Context

Stack Overflow Q#2795023, score: 686

Revisions (0)

No revisions yet.