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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:19:10+00:00 2026-05-21T11:19:10+00:00

suppose I have an object like this: class Spline { public: Spline(std::size_t const dim);

  • 0

suppose I have an object like this:

class Spline {
public:
    Spline(std::size_t const dim);
    // Quite a few functions here. One of those:
    vec operator()(double const t) const; // Returns vector of dimension d
}

Now, at most uses of this class, the dimension will already be determined at compile time, thus it would be a good idea (for performance reasons) to change the class like this:

template <std::size_t dim>
class Spline {
public:
    Spline();
    // Quite a few functions here. One of those:
    vec::fixed<dim> operator()(double const t) const; // Returns vector of dimension d
}

(For those who wonder, vec and vec::fixed are objects defined by the armadillo linear algebra library). Now I would like to have both versions living in parallel, thus being able to choose the dimension at compile time as well as during runtime. In short, I would like to create the equivalent of vec::fixed<dim> as Spline::fixed<dim>, but without implementing all functions twice. Especially, I would have to choose the return type of all those functions depending on whether there is a template argument present or not.

Do you have any idea how I might accomplish this, especially thinking in terms of a clear and maintainable design? (In the hope that I made myself clear, which I am not totally sure about.)

  • 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-21T11:19:10+00:00Added an answer on May 21, 2026 at 11:19 am

    Use a simple traits metastruct and specialize that.

    template<std::size_t dim>
    struct spline_return_traits{
      typedef vec::fixed<dim> type;
    };
    
    template<>
    struct spline_return_traits<0>{ // 0 is a special marker for runtime dimensions
      typedef vec type;
    };
    
    template<std::size_t dim>
    class Spline_impl{
      typedef typename spline_return_traits<dim>::type spline_return;
    public:
      spline_return operator()(double const t) const;
    // if <dim> is 0, then the dynamic vec will be chosen as the return type
      // all your functions
    };
    
    class Spline : public Spline_impl<0>{ // default is dynamic
    public:
      template<int dim>
      struct fixed : public Spline_impl<dim>{
      };
    };
    

    Now you simple use that. 🙂 Every operator, constructor and function of Spline_impl should be available in the subclasses. For the implementation of each function, you need to do some branching where it’s a must to decide between runtime or fixed vec:

    if(dim == 0){
      // runtime specific stuff
    }else{
      // compile-time specific stuff
    }
    

    Use as:

    Spline dynamic_spline;
    Spline::fixed<10> fixed_10_spline;
    

    Only problem being that the Spline class will be double the size of Spline_impl… :/ Lemme think if I can find a solution to that too.
    Edit: If you don’t want Spline to be double the size of Spline_impl, one possibility is to add a little verbosity and a typedef:

    class Spline : public Spline_impl<0>{ // default is dynamic size
    public:
      template<std::size_t dim>
      struct fixed{
        typedef Spline_impl<dim> type;
      };
    };
    

    And use as

    Spline dynamic_spline;
    typename Spline::fixed<10>::type fixed_10_spline;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some code like this: class Base { public: virtual int Foo(int)
Let's suppose I have this object: [Serializable] public class MyClass { public int Age
Suppose I have a simple Java class like this: public class User { String
Suppose I have an object that looks like: public class Obj { String foo;
Suppose I have a design like this: Object GUI has two objects: object aManager
Hi Suppose I have a simple model class like this: class TestModel(models.Model): testkey =
Suppose that I have a class like this class Employee: pass I create two
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
Suppose I have a namedtuple like this: EdgeBase = namedtuple(EdgeBase, left, right) I want

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.