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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:32:17+00:00 2026-05-27T19:32:17+00:00

I would like to create a generic vector class and create specializations for a

  • 0

I would like to create a generic vector class and create specializations for a few cases. Something like this (it does not compile, but hopefully communicates my intentions):

template<int dim, typename T = float>
class Vector
{
public:
    typedef Vector<dim, T> VecType;

    Vector() { /**/ }
    Vector(const VecType& other) { /**/ )
    Vector& operator=(const VecType& other) { /**/ }

    VecType operator+(const VecType& other) { /**/ }    
    VecType operator-(const VecType& other) { /**/ }    
    T operator*(const VecType& other) { /**/ }

private:
    std::array<T, dim> elements;
};

template<int dim, typename T>
class Vector<2>
{
public:
    T x() const { return elements[0]; }
    T y() const { return elements[1]; }
};

template<int dim, typename T>
class Vector<3>
{
public:
    T x() const { return elements[0]; }
    T y() const { return elements[1]; }
    T z() const { return elements[2]; }
};

In other words, I want the default type of the elements to be float and I want to have x() and y() accessor methods for the dim = 2 case, and x(), y() and z() for the dim = 3 case. I’m a little confused by the error messages:

vector.h:56:10: error: declaration of ‘int dim’

vector.h:6:10: error: shadows template parm ‘int dim’

(same for T).

How can I do this correctly? (if it’s possible)

  • 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-27T19:32:18+00:00Added an answer on May 27, 2026 at 7:32 pm

    1.

    When partially specializing a template, only supply the template parameters that are actually a parameter. Since you’ve already fixed dim to be 2 or 3, there’s no need to specify it again.

    template<typename T>
    class Vector<2, T>
    {
       ....
    

    2.

    Specializing a class really means changing the whole declaration. Therefore, the members s of the generic Vector<dim, T> will not be available in the specialized Vector<2, T>. You could make the generic Vector<dim, T> as into an internal base class, and create a subclass just for specialization:

    template<int dim, typename T>
    class VectorImpl;
    
    ...
    
    template<int dim, typename T = float>
    class Vector : public VectorImpl<dim, T> {};
    
    template<typename T>
    class Vector<2, T> : public VectorImpl<2, T>
    {
    public:
       T x() const { ... }
    };
    

    3.

    You don’t need to define VecType! Inside a template, you could just use Vector. It will automatically be deduced to refer to the class with the right parameters.

    The end result that compiles:

    #include <array>
    
    template<int dim, typename T>
    class VectorImpl
    {
    public:
        //typedef Vector<dim, T> VecType;
    
        VectorImpl() {  }
        VectorImpl(const VectorImpl& other) {  }
        VectorImpl& operator=(const VectorImpl& other) { return *this; }
    
        VectorImpl operator+(const VectorImpl& other) { return *this; }
        VectorImpl operator-(const VectorImpl& other) { return *this; }
        T operator*(const VectorImpl& other) { return 0; }
    
    protected:
        std::array<T, dim> elements;
    };
    
    template <int dim, typename T = float>
    class Vector : public VectorImpl<dim, T> {};
    
    template<typename T>
    class Vector<2, T> : public VectorImpl<2, T>
    {
    public:
        T x() const { return this->elements[0]; }
        T y() const { return this->elements[1]; }
    };
    
    template<typename T>
    class Vector<3, T> : public VectorImpl<2, T>
    {
    public:
        T x() const { return this->elements[0]; }
        T y() const { return this->elements[1]; }
        T z() const { return this->elements[2]; }
    };
    
    int main()
    {
        Vector<2> v;
        Vector<3> vv;
        v + v;
        vv.z();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a method in a base generic class to return
I would like to create a generic query from a generic class T. Is
I would like to create a generic job class which takes some arguments instead
I would like to create an SSL connection for generic TCP communication. I think
I would like to create a generic logging solution for my stored procedures, allowing
I need to create a generic type, but I do not know the type
I would like to create a generic update method in a ObjectContext, using a
I have a Powershell script where I would like to create a generic List
I would like to create a few overloaded methods which accept a Func parameter.
I would like to create a map that uses a class as a key

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.