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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:42:38+00:00 2026-05-11T12:42:38+00:00

Considering the following two usage scenarios (exactly as you see them, that is, the

  • 0

Considering the following two usage scenarios (exactly as you see them, that is, the end-user will only be interested in using Vector2_t and Vector3_t):

[1]Inheritance:

template<typename T, size_t N> struct VectorBase { };  template<typename T> struct Vector2 : VectorBase<T, 2> { };  template<typename T> struct Vector3 : VectorBase<T, 3> { };  typedef Vector2<float> Vector2_t; typedef Vector3<float> Vector3_t; 

[2]Specialization:

template<typename T, size_t N> struct Vector { };  template<typename T> struct Vector<T, 2> { };  template<typename T> struct Vector<T, 3> { };  typedef Vector<float, 2> Vector2_t; typedef Vector<float, 3> Vector3_t; 

I can’t make up my mind as to which is a nicer solution. The obvious advantage to inheritance is code reuse in the derived classes; a possible disadvantage being performance (bigger size, users may pass by value, etc). Specialization seems to avoid all that, but at the expense of me having to repeat myself multiple times.

What other advantages/disadvantages did I miss, and in your opinion, which route should I take?

  • 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. 2026-05-11T12:42:38+00:00Added an answer on May 11, 2026 at 12:42 pm

    What you ultimately want, i think, is to have the user type

    Vector<T, N> 

    And depending on N, the user will get slight different things. The first will not fulfill that, but the second will, on the price of code duplication.

    What you can do is to invert the inheritance:

    template<typename T, size_t N> struct VectorBase  { };  template<typename T> struct VectorBase<T, 2> { };  template<typename T> struct VectorBase<T, 3> { };  template<typename T, size_t N> struct Vector : VectorBase<T, N> { }; 

    And implement the few functions that depend only on N being some specific value in the appropriate base-class. You may add a protected destructor into them, to prevent users deleting instances of Vector through pointers to VectorBase (normally they should not even be able to name VectorBase: Put those bases in some implementation namespace, like detail).

    Another idea is to combine this solution with the one mentioned in another answer. Inherit privately (instead of publicly as above) and add wrapper functions into the derived class that call the implementations of the base-class.

    Yet another idea is to use just one class and then enable_if (using boost::enable_if) to enable or disable them for particular values of N, or use a int-to-type transformer like this which is much simplier

    struct anyi { }; template<size_t N> struct i2t : anyi { };  template<typename T, size_t N> struct Vector {     // forward to the 'real' function     void some_special_function() { some_special_function(i2t<N>()); }  private:     // case for N == 2     void some_special_function(i2t<2>) {         ...     }      // case for N == 3     void some_special_function(i2t<3>) {         ...     }      // general case     void some_special_function(anyi) {         ...     } }; 

    That way, it is completely transparent to the user of Vector. It also won’t add any space overhead for compilers doing the empty base class optimization (quite common).

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

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer As others have noted, the number of tables is probably… May 11, 2026 at 2:11 pm
  • added an answer What you need is a Composite Application Block: Winforms: http://msdn.microsoft.com/en-us/library/aa480450.aspx… May 11, 2026 at 2:11 pm
  • added an answer It looks to me like you can't connect to a… May 11, 2026 at 2:11 pm

Related Questions

Considering the following architecture: a base object 'Entity' a derived object 'Entry:Base' and a
I'm considering the following: I have some data stream which I'd like to protect
Considering the criteria listed below, which of Python, Groovy or Ruby would you use?
Considering the full list of PDC videos published here what are, in your opinion,
After considering the answers to my previous question ( One SVN Repository or many?
For so long, I've read and understood the following truths concerning web development: HTML
Or more generally, how do I remove an item from a colon-separated list in
I'm working on improving our group's development process, and I'm considering how best to
The MSDN documentation is (somewhat) clear about the following two facts about GDI Pens:

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.