Prajwal_7
Prajwal_7

Reputation: 129

Template initialization:

I want to create row vector and column vector alias from Matrix class. How can I do that?

template<class T, unsigned int m, unsigned int n>
class Matrix {
public:
    Matrix();
        
    .
    .
    .

private:
    unsigned int rows;
    unsigned int cols;
    .
};

I get error here. I see that type alias for templates cannot be done. Is there any way I can handle it? For below I get error as "Partial specialization of alias template".

template<class T, unsigned int m, unsigned int n>
using rowVector<T,n> = Matrix<T,1,n>;

template<class T, unsigned int m, unsigned int n>
using colVector<T,m> = Matrix<T,m,1>;

Any pointers how can I achieve this?

Upvotes: 1

Views: 157

Answers (2)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 123468

I believe you must have more code than what you posted, because this

template<class T, unsigned int m, unsigned int n>
class Matrix {};

template<class T, unsigned int m, unsigned int n>
using rowVector<T,n> = Matrix<T,1,n>;

template<class T, unsigned int m, unsigned int n>
using colVector<T,m> = Matrix<T,m,1>;

Causes the following error

prog.cc:5:16: error: expected '=' before '<' token
 using rowVector<T,n> = Matrix<T,1,n>;
                ^
prog.cc:5:16: error: expected type-specifier before '<' token
prog.cc:8:16: error: expected '=' before '<' token
 using colVector<T,m> = Matrix<T,m,1>;
                ^
prog.cc:8:16: error: expected type-specifier before '<' token

The correct syntax for an alias template is:

template < template-parameter-list >
using identifier attr(optional) = type-id ;

So the fix is

template<class T, unsigned int m, unsigned int n>
using rowVector = Matrix<T,1,n>;

template<class T, unsigned int m, unsigned int n>
using colVector = Matrix<T,m,1>;

And I suppose you want to remove m as parameter of rowVector and n as parameter of colVector:

template<class T, unsigned int n>
using rowVector = Matrix<T,1,n>;

template<class T, unsigned int m>
using colVector = Matrix<T,m,1>;

Upvotes: 1

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 30005

This is the correct syntax:

template <class T, unsigned int n>
using rowVector = Matrix<T, 1, n>;

template <class T, unsigned int m>
using colVector = Matrix<T, m, 1>;

Upvotes: 3

Related Questions