user8953650
user8953650

Reputation: 1020

Using alias for template class

I didn't find any answer about this question : I'm writing a template class DenseMatrix , the necessity of using the specification "Dense" before Matrix is because this class in in a hierarchy that have at the top the base abstract class Matrix from which derives SparseMatrix (from which derive al lot of class representing the different way of storage for sparse matrix, like CRS , MCRS, BlockCSR .. ) Now I wat make available for example in main function the possibility to instantiate an object of the class DenseMatrix using the simple name matrix (note that is not Matrix like the abstract base class) , so I remember that in C was possible used

typdef struct {

} name ;

then

name obj ; // instance of struct  

I want obtain the same thing but in a C++ class (C++11 oriented) which is the best way to doing that directly in the header of the DenseMatrix class declaration ? P.S. during the defining of the method I always use DenseMatrix:: and not the alias

edit here the example , looking at the end of code

# include "Matrix.H"

template <typename Type>
class DenseMatrix ;

template<typename U>
std::ostream& operator<<(std::ostream& os, const DenseMatrix<U>& m )



    template <typename Type>
    class DenseMatrix 
                               :     public Matrix<Type> 
    {


           template<typename U>
           friend std::ostream& operator<<(std::ostream& os, const DenseMatrix<U>& m );

    //--
    //
       public:

           constexpr DenseMatrix (std::initializer_list<std::vector<Type>> ) noexcept ;

           constexpr DenseMatrix (const std::string& );

     //       constexpr DenseMatrix (std::size_t , std::size_t);

           virtual  ~DenseMatrix() = default ;

           Type& operator()(const std::size_t , const std::size_t) noexcept override;

           const Type& operator()(const std::size_t , const std::size_t ) const noexcept override;

           void constexpr print () const noexcept override ;

           auto constexpr size1()const noexcept { return Rows ; }

           auto constexpr size2()const noexcept { return Cols ; }

           Type constexpr findValue(const std::size_t , const std::size_t ) const noexcept ;

       protected:

           std::vector<Type> data ;

           std::size_t Rows ;
           std::size_t Cols ;

           mutable Type dummy ;
    } ;
// here ------\/ -------
template <typename T>
using matrix<T> = DenseMatrix<T>

thanks @R2RT it's what I was looking for ! SOLVED

Upvotes: 0

Views: 304

Answers (1)

R2RT
R2RT

Reputation: 2146

If I get you right then you are almost here, but you have one <T> too much:

template <typename T>
using matrix = DenseMatrix<T>;

It's called template aliasing

An alias template is a template which, when specialized, is equivalent to the result of substituting the template arguments of the alias template for the template parameters in the type-id

http://en.cppreference.com/w/cpp/language/type_alias

Upvotes: 1

Related Questions