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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:39:51+00:00 2026-05-24T08:39:51+00:00

I am designing class, which is almost the same for 2d and 3d, so

  • 0

I am designing class, which is almost the same for 2d and 3d, so I am trying to use templates, to create it as one class typed by vector type (2d od 3d)

Some methods and member are very same for 2d and 3d

Some (but very few) methods are slightly different, I use trait here – see doSomething()

Some members and method are present for 3d but are not for 2d (up vector) and THAT IS MY PROBLEM.

I can solve it by complete class specialization, but is there any other way, how to include/exclude these members without specialization whole class?

I have trait:

template<typename T>
struct VectorInfo{};

template<>
struct VectorInfo<Vec2>
{
    enum { dim = 2 };
};

template<>
struct VectorInfo<Vec3>
{
    enum { dim = 3 };
};

template<int Val>
struct VectorDimension
{
    enum { val = Val };
};

and Class:

template <typename vector_type>
class LocalSpace
{
public:
    ////////////////////////////////////////
    //Common for 2D and 3D
    const vector_type & getSideVector() const;      
    void setSideVector(const vector_type & s);

    const vector_type & getForwardVector() const;
    void setForwardVector(const vector_type & f);

    const vector_type & getPosition() const;
    void setPosition(const vector_type & p);

    bool isRightHanded() const;

    //others methods...

    //////////////////////////////////////////
    //only for 3D
    const vector_type & getUpVector() const;    
    void setUpVector(const vector_type & u);   

    //One of few methods differing for 2D and 3D 
    inline void doSomething(const vector_type & v)      
    {
        doSomethingImpl(v, VectorDimension<VectorInfo<vector_type>::dim>);
    }

protected:

    void doSomethingImpl(const vector_type & v, VectorDimension<2>)
    {
    }

    void doSomethingImpl(const vector_type & v, VectorDimension<3>)
    {
    }

private:
    vector_type m_side;     //2d+3d        
    vector_type m_forward;  //2d+3d
    vector_type m_up;       //3d ONLY

    vector_type m_position; //2d+3d
};

Hope you understand my question.

EDIT:
thank you for your responses,
now I have

struct BlankType{};

template <typename vector_type>
class LocapSpace3DBase
{
public:
    const vector_type & getUpVector() const;
    void setUpVector(const vector_type & u);
private:
    vector_type m_up;
};

template <typename vector_type>
class LocalSpace : public boost::mpl::if_c<
                                                            VectorInfo<vector_type>::dim == 3,
                                                            LocapSpace3DBase<vector_type>,
                                                            BlankType>::type

Is there any way, to get rid of the BlankType? Like – if the dimension is 3, derive from 3DBase, if not, DO NOTHING (instead of derive from empty struct)?

  • 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-24T08:39:51+00:00Added an answer on May 24, 2026 at 8:39 am

    You can derive from different base classes using mpl::if_ or mpl::if_c. In the branch for 3d you can declare the members and methods needed only for 3d case.

    Something like this:

    class LocalSpaceBase2
    {
        vector_type m_up;
        // ...
    };
    
    template <typename vectortype>
    class LocalSpace : public boost::mpl::if_c<
                                  boost::is_same<vectortype, Vec3>,
                                  LocalSpaceBase3,
                                  LocalSpaceBase2>::type
    {
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am designing a class where some methods won't cause any harm if they
In designing a class for customer for example would it make sense to use
Suppose I am designing a class that can handle any database technology to create
When designing both the domain-model and class-diagrams I am having some trouble understanding what
I'm designing a system in java which utilizes a dns lookup class. My question
I'm designing some new class diagrams for extending an existing office automation application. In
I am designing a template class Policy which needs to be able to handle
I am designing a class which comprises of objects of other classes, right now
I am working with models of neurons. One class I am designing is a
I am designing an entity class which has a field named documentYear, which might

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.