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 5953059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:46:14+00:00 2026-05-22T17:46:14+00:00

I am building a matrix library and I am trying to use the policy-based

  • 0

I am building a matrix library and I am trying to use the policy-based design.
So my base classes are classes that provide a storage method and some
access functions.
I also have a function matrix which provides the mathematical functions.
This works great, but there is a major problem with the operator*
because of the return type. I will explain it with some code.

Base class that provides a stack storage :

template < typename T, unsigned int rows, unsigned int cols>
class denseStackMatrix {
public:
    typedef T value_type;

private:
    value_type grid[rows][cols];
    const unsigned int rowSize;
    const unsigned int colSize;

Then I have my matrix class which provides mathematical functionality :

template <typename MatrixContainer >
class matrix : public MatrixContainer {
public:
    typedef MatrixContainer Mcontainer;

    matrix<Mcontainer>& operator +(const matrix<Mcontainer>&);
    matrix<Mcontainer>& operator *(const matrix<Mcontainer>&);

operator+ always works, operator* only works for square matrix.
So we still need one for all matrices. And that’s were it goes
wrong. I have already tried few things, but nothings works.
I look for something like this, with the help of c++0x (usage of
c++0x is not a requirement)
you shall notice the “???” 🙂

friend auto operator * (const matrix<T1>& matrix1, const matrix<T2>& matrix2)
-> decltype(matrix<???>);

An example of the problem

matrix<denseStackMatrix<int,3,2> > matrix1;
matrix<denseStackMatrix<int,2,4> > matrix2;
matrix<denseStackMatrix<int,3,4> > matrix3 = matrix1 * matrix2;

Here it will complain about the type, because it does not match any of the two parameter types. But the compiler needs to know the type at compile-time and I do not know how to provide it.

I know there are other options for the design, but I am really looking for a solution for this scenario..

Thank you !

  • 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-22T17:46:15+00:00Added an answer on May 22, 2026 at 5:46 pm

    Picking up on the idea of @hammar, but with partial specialization to allow the normal syntax like the question shows:

    template<class MatrixContainer>
    class matrix;
    
    template<
      template<class,int,int> class MatrixContainer,
      class T, int rows, int cols
    >
    class matrix< MatrixContainer<T,rows,cols> >{
      typedef MatrixContainer<T,rows,cols> Mcontainer;
      typedef matrix<Mcontainer> this_type;
      static int const MyRows = rows;
      static int const MyCols = cols;
    
    public:
      template<int OtherCols>
      matrix<MatrixContainer<T,MyRows,OtherColls> > operator*(matrix<MatrixContainer<T,MyCols,OtherCols> > const& other){
        typedef matrix<MatrixContainer<T,MyCols,OtherCols> > other_type;
        typedef matrix<MatrixContainer<T,MyRows,OtherCols> > result_type;
        // ...
      }
    };
    

    Edit: As you said in your comment, you can also use this to create a matrix that doesn’t use a MatrixContainer which has row and column size as template parameters:

    template<
      template<class> class MatrixContainer,
      class T
    >
    class matrix< MatrixContainer<T> >{
      typedef MatrixContainer<T> Mcontainer;
      typedef matrix<Mcontainer> this_type;
    
    public:
      // normal matrix multiplication, return type is not a problem
      this_type operator*(this_type const& other){
        // ensure correct row and column sizes, e.g. with assert
      }
    
      // multiply dynamic matrix with stack-based one:
      template<
        template<class,int,int> class OtherContainer,
        int Rows, int Cols
      >
      this_type operator*(matrix<OtherContainer<T,Rows,Cols> > const& other){
        // ensure correct row and column sizes, e.g. with assert
      }
    };
    

    Usage:

    // stack-based example
    matrix<DenseStackMatrix<int,3,2> > m1;
    matrix<DenseStackMatrix<int,2,4> > m2;
    matrix<DenseStackMatrix<int,3,4> > m3 = m1 * m2;
    
    // heap-based example
    matrix<DenseHeapMatrix<int> > m1(3,2);
    matrix<DenseHeapMatrix<int> > m2(2,4);
    matrix<DenseHeapMatrix<int> > m3 = m1 * m2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a prototype for a quantitative library that does some signal analysis
Im trying to stupid the ways of building a game. One issue that i
I am building a class that has a union for its matrix data, however,
I'm building a Matrix class for all the operations such as addition, subtraction, multiplication,
building a site using PHP and MySQL that needs to store a lot of
Building a website that has English & Japanese speaking users, with the Japanese users
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
Building a search with some custom objects and three scopes: All , Active ,
Building an iPhone OS application that will allow users to anonymously post information to
been trying out this for quite some time but I'm still unable to built

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.