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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:02:09+00:00 2026-06-11T01:02:09+00:00

in the problem i am facing i need something which works more or less

  • 0

in the problem i am facing i need something which works more or less like a polymorphic class, but which would allow for virtual template methods.

the point is, i would like to create an array of subproblems, each one being solved by a different technique implemented in a different class, but holding the same interface, then pass a set of parameters (which are functions/functors – this is where templates jump up) to all the subproblems and get back a solution.

if the parameters would be, e.g., ints, this would be something like:

struct subproblem
{
...
virtual void solve (double& solution, double parameter)=0; 
}
struct subproblem0: public subproblem
{
...
virtual void solve (double& solution, double parameter){...}; 
}
struct subproblem1: public subproblem
{
...
virtual void solve (double* solution, double parameter){...}; 
}

int main{
subproblem problem[2];
subproblem[0] = new subproblem0();
subproblem[1] = new subproblem1();
double argument0(0),
       argument1(1),
       sol0[2],
       sol1[2];
for(unsigned int i(0);i<2;++i)
    {
    problem[i]->solve( &(sol0[i]) , argument0);
    problem[i]->solve( &(sol1[i]) , argument1);
    }
return 0;
}

but the problem is, i need the arguments to be something like

Arg<T1,T2> argument0(f1,f2)

and thus the solve method to be something of the likes of

template<T1,T2> solve (double* solution, Arg<T1,T2> parameter)

which cant obviously be declared virtual ( so cant be called from a pointer to the base class)…

now i’m pretty stuck and don’t know how to procede…

  • 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-11T01:02:10+00:00Added an answer on June 11, 2026 at 1:02 am

    Typically, this type of problem is solved with a public function template and a protected virtual function taking in a type-erasure object. For your specific problem, this could translate into this:

    struct param_list_base {
      virtual double getParam(int i) const = 0;
    };
    
    template <typename ParamStorage>
    struct param_list : param_list_base {
      const ParamStorage& params;
      param_list(const ParamStorage& aParams) : params(aParams) { };
      virtual double getParam(int i) const {
        return params[i];
      };
    };
    
    class subproblem {
      protected:
        virtual void solve_impl(double* sol, param_list_base* params) = 0;
      public:
        template <typename ParamStorage>
        void solve(double* sol, ParamStorage params) {
          param_list<ParamStorage> tmp(params);
          solve_impl(sol, &tmp);
        };
    };
    

    And then, you can also specialize the param_list template for different kinds of parameter storage objects. For functors, you can do the same. This is, of course, somewhat limited, but it usually can be made to work in most cases. Otherwise, you can still rely on a similar pattern of protected versus public functions. For example, you can have a set of non-template virtual functions in the public interface, which all just call a private function template (one implementation for all the cases), this isn’t pretty but it works if the number of possible template arguments is limited.

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

Sidebar

Related Questions

I'm playing with kretprobes and I am facing a problem. I would like, in
I'm currently facing a more mathematical problem. I'm developing an application which is interested
I'm facing a problem when I need to distribute my java application, created using
I'm facing a problem with Regex... I had to match sharepoint URL.. I need
I'm facing the following problem: in the controller I select the data I need
I am facing the following problem. Let's say I have a file, which contains
This is something I would need to do to create a flexible logic structure
I am facing a problem with CollapsePanel. I need to change it's title when
I am facing problem. I need to build one app in two ways, first
I am facing problem in DateTime converstion. My input is 09/22/2011, I need to

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.