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

  • Home
  • SEARCH
  • 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 8172841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:54:35+00:00 2026-06-06T21:54:35+00:00

This occured in the line of thought following Template specialization or conditional expressions? .

  • 0

This occured in the line of thought following Template specialization or conditional expressions?.

I am using template specialisation in a project of mine and came across this example from Stroustrup: Matrix.h, where he declares a MatrixBase template class

template<class T> class Matrix_base

for common elements and a Matrix template class

template<class T = double, int D = 1> class Matrix

as a “prop” (whatever that is) for specialisations. He declares the constructor as private so that only specialisations can be instanciated. These are declared:

template<class T> class Matrix<T,1> : public Matrix_base<T> {...};
template<class T> class Matrix<T,2> : public Matrix_base<T> {...};
template<class T> class Matrix<T,3> : public Matrix_base<T> {...};

My question is: In this case, what is the advantage of specialisation? Obviously there is no code that the three specialisations have in common, so why not cut out the general template and declare:

template<class T> class Matrix_1<T> : public Matrix_base<T> {...};
template<class T> class Matrix_2<T> : public Matrix_base<T> {...};
template<class T> class Matrix_3<T> : public Matrix_base<T> {...};

?

  • 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-06-06T21:54:39+00:00Added an answer on June 6, 2026 at 9:54 pm

    Because by having the second template parameter, one allows for specializations as well as a general, non-specialized implementation. So

    Matrix<float, 1000> m;
    

    might do something reasonable but non specialized, whereas you would have to define a Matrix_1000<T>.
    Edit: the first point applies in general, but not to this particular case, where the general case has a private constructor.

    Furthermore, it allows you to do stuff like

    Matrix<double, SOME_CONSTANT> m;
    

    which you cannot do with your _N solution.

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

Sidebar

Related Questions

I'm reading Stroustrup's Programming: Principles and Practice and came across this code: int main()
I got this error An error occurred while parsing EntityName. Line 1, position 61.
This question occured to me while programming a Android application, but it seems to
When error occured Python prints something like this: Traceback (most recent call last): File
I encountered this error: Cannot implicitly convert type System.Web.UI.WebControls.LoginView to Login The error occured
I've needed this a few times, and only now it occured to me, that
I have the following code in my application: using (var database = new Database())
I am working on a codeigniter project. I developed it using wampserver locally, but
I'm trying to compile a project using appcelerator (crossplatform dev for iphone/android). Just evaluating
I'm converting classic ASP over to ASP.Net, and came up with this error for

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.