Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5967319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:55:36+00:00 2026-05-22T19:55:36+00:00

I want to use partial specialization for a template class so that all children

  • 0

I want to use partial specialization for a template class so that all children of that template class will use that specialization. Let me explain it with an example 🙂

template < typename T, unsigned int rows, unsigned int cols>
class BaseMatrix {...};

This class will have children that specify the structure of the matrix, like sparse,dense,diagonal,..

template < typename T, unsigned int rows, unsigned int cols>
class DiagonalMatrix : public BaseMatrix<T,rows,cols>{..}

Then those classes will have children again that specify the storage : stack arrays, vectors, list, queues,..

template < typename T, unsigned int rows, unsigned int cols>
class StackDiagonalMatrix : public DiagonalMatrix<T, rows, cols> {..}

Then there is a class Matrix, which provides all the mathematical functionality. This template class implements operator+, operator-, etc…

   template <typename T, 
             template<typename, unsigned, unsigned> class MatrixContainer,
             unsigned Rows, 
             unsigned Cols>
    class matrix;

For this last class I want to write specializations like this:

template <typename T,unsigned Rows, unsigned Cols>
class matrix<T, BaseMatrix, Rows, Cols> {};

template <typename T,unsigned Rows, unsigned Cols>
class matrix<T, DiagonalMatrix, Rows, Cols>  {};

But when I write a StackDiagonalMatrix which inherits from DiagonalMatrix, it does not find the specialization for DiagonalMatrix — it does not find a specialization at all actually.

error: aggregate ‘matrix<int, StackDenseMatrix, 3u, 2u> matrix’ has incomplete type and cannot be defined

Now is there a solution for this problem? Can you write a specialization for a parent of several template classes?

Many thanks!

Full Source that is involved :

    template <typename T, unsigned int rows, unsigned int cols>
    class BaseMatrix {
    protected:
        BaseMatrix(){};
        static const unsigned rowSize = rows;
        static const unsigned colSize = cols;
    };

    template <typename T, unsigned int rows, unsigned int cols>
    class DenseMatrix : public BaseMatrix<T, rows, cols> {
    protected:
        DenseMatrix(){};

    };

    template <typename T, unsigned int rows, unsigned int cols>
    class StackDenseMatrix : public DenseMatrix<T, rows, cols> {

    public:
        typedef T                                           value_type;

    private:
        value_type                                          grid[rows][cols];
        StackDenseMatrix();
    };

    template<typename value_type, unsigned int rows, unsigned int cols>
    StackDenseMatrix<value_type, rows,cols>::StackDenseMatrix () {
        for (unsigned int i = 0; i < this->rowSize; i++) {
            for (unsigned int j = 0; j < this->colSize; j++) {
                grid[i][j] = 0;
            }
        }
    }

    template <typename T, template<typename, unsigned, unsigned> class MatrixContainer ,unsigned Rows, unsigned Cols>
    class matrix;

    template <typename T,unsigned Rows, unsigned Cols>
    class matrix<T,BaseMatrix, Rows, Cols> {
         matrix(){};
};

int main () {
    matrix<int, StackDenseMatrix, 3, 2> matrix;
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:55:37+00:00Added an answer on May 22, 2026 at 7:55 pm

    Inheritance does not apply to template specializations. When you invoke matrix<int,StackDenseMatrix,3,2> matrix;, it will select the general template for matrix, because the second argument is StackDenseMatrix, not BaseMatrix. Even though those classes are related through inheritance, it doesn’t make any difference, they are not of the exact same type, so the compiler won’t select the specialization of matrix.

    To solve your problem, I don’t think inheritance will do you any good in this case. In generic programming, the more appropriate tools are type-traits, policies and concepts. In this case, you should be able to apply some type-traits to achieve similar goals. One trick I like to use is the default template argument that depends on a previous template argument, and then do a partial specialization. As follows for example:

    enum MatrixStorage {
      DenseMatrix,
      SparseMatrix
    };
    
    enum MatrixStructure {
      GeneralMatrix,
      SquareMatrix,
      DiagonalMatrix  //, ...
    };
    
    template <typename T, unsigned Rows, unsigned Cols>
    class StackDenseMatrix {
      public: 
        typedef T value_type;
        static const MatrixStorage Storage = DenseMatrix;
        static const MatrixStructure Structure = GeneralMatrix;
      //..
    };
    
    //General template with default arguments:
    template <typename T, 
              template <typename, unsigned, unsigned> class MatrixContainer,
              unsigned Rows, unsigned Cols, 
              MatrixStorage Storage = MatrixContainer<T,Rows,Cols>::Storage,
              MatrixStructure Structure = MatrixContainer<T,Rows,Cols>::Structure>
    class matrix;
    
    //Specialization with given arguments:
    template <typename T, 
              template <typename, unsigned, unsigned> class MatrixContainer,
              unsigned Rows, unsigned Cols>
    class matrix<T,MatrixContainer,Rows,Cols,DenseMatrix,GeneralMatrix> {
      //implementation of matrix for when the container is dense and has general structure...
    };
    
    int main() {
      matrix<int,StackDenseMatrix,3,2> M; //no extra arguments, and the right specialization will be selected based on the traits of StackDenseMatrix.
      return 0;
    };
    

    I have my own matrix library that heavily relies on template meta-programming and generic programming techniques along the lines of the above example to provide special implementation of matrix operations for different type of matrix structures and storage, and it works very well this way. I used to use inheritance for the different matrix types, but I have now switched to relying only on type-traits, concepts, policies and Sfinae switches, that is a much more practical solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the MultipleLookupField control in a web page that will run
I want to use a temp directory that will be unique to this build.
Below is my stored procedure. I want use stored procedure select all row of
I want to partially specialize an existing template that I cannot change ( std::tr1::hash
I have a scenario I want to use a partial view but I'm having
I have a strongly typed Person view, that I want to render a partial
I want to use Telerik DatePickers and IntegerTextBoxes on my Partial View loaded via
I want to use a partial view to represent the rows of a table:
I want to use partial views with AJAX calls in ASP.NET MVC, and this
I want to use jQuery get method with a partial view. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.