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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:48:33+00:00 2026-05-12T07:48:33+00:00

I have a matrix class with the size determined by template parameters. template <unsigned

  • 0

I have a matrix class with the size determined by template parameters.

template <unsigned cRows, unsigned cCols>
class Matrix {
    ...
};

My program uses matrices of a few sizes, typically 2×2, 3×3, and 4×4. By setting the matrix size with template parameters rather than run-time parameters allows the compiler to do a lot of inlining and optimization.

But now I need a member function that returns a new matrix that has one fewer row and one fewer column.

Matrix<cRows - 1, cCols - 1> Reduced(unsigned row, unsigned col) const { ... }

The idea is that that it will return a matrix with the specified row and column deleted. In practice, this will only ever be called with a matrix that has at least three rows and three columns, returning a 2×2 at the smallest.

The compiler doesn’t see the lower bound, so it gets stuck in an infinite recursion trying to instantiate the templates with ever decreasing sizes. I tried putting two clues in the function itself that these smaller sizes cannot occur:

Matrix<cRows - 1, cCols - 1> Reduced(unsigned row, unsigned col) const {
    static_assert(cRows > 1 && cCols > 1);
    if (cRows <= 1 || cCols <= 1) throw std::domain_error();
    Matrix<cRows - 1, cCols - 1> r;
    // ... initialize r ...
    return r;
}

Neither the static_assert nor the if-statement seems to be a strong enough clue to the compiler that a 0x0 matrix will never be generated. (Ironically, it does complain about the if-statement having a constant compile-time condition.)

Does anyone have any suggestions on how to avoid this compile-time infinite recursion?

  • 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-12T07:48:33+00:00Added an answer on May 12, 2026 at 7:48 am

    You need to provide a specialization for a Matrix that has no rows or no columns.

    E.g.

    template<unsigned cRows>
    class Matrix< cRows, 0 >
    {
        Matrix<cRows - 1, 0> Reduced() { return Matrix<cRows - 1, 0>(); }
    };
    
    
    template<unsigned cCols>
    class Matrix< 0, cCols >
    {
        Matrix<0, cCols - 1> Reduced() { return Matrix<0, cCols - 1>(); }
    };
    
    
    template<>
    class Matrix< 0, 0 >
    {
        Matrix<0, 0> Reduced() { return Matrix<0, 0>(); }
    };
    

    The issue you have is that attempting to instantiate the Matrix Reduced function with a particular set of template parameters always required instantiating the Matrix template for a different set of parameters (cRows – 1, cCols -1). This recursion has to be stopped somewhere. If you are only ever dealing with square matrices, then you can get away with fewer specializations.

    Also, you can could stop the recursion with a completely empty class if you are never going to use, say, a 1×1 matrix, the result of reduce on a 2×2 matrix.

    template<>
    class Matrix< 1, 1 > {};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 199k
  • Answers 199k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This MSDN article describes this and other new features that… May 12, 2026 at 7:46 pm
  • Editorial Team
    Editorial Team added an answer You use GetTickCount function see this example. program Ticks; {$APPTYPE… May 12, 2026 at 7:46 pm
  • Editorial Team
    Editorial Team added an answer If you're finding build-busting checkins too late in the game… May 12, 2026 at 7:46 pm

Related Questions

One of the assignments in my algorithms class is to design an exhaustive search
I am currently working with using Bezier curves and surfaces to draw the famous
I'm working on a sparse matrix class that needs to use an array of
I'm writing this question with reference to this one which I wrote yesterday. After

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.