Alex Rivera | Logout

Can template polymorphism be used in place of OO polymorphism?

Asked 2009-07-31T15:55:54.053
25

I am trying to get my head around applying template programming (and at some future point, template metaprogramming) to real-world scenarios. One problem I am finding is that C++ Templates and Polymorphism don't always play together the way I want.

My question is if the way I'm trying to apply template programming is improper (and I should use plain old OOP) or if I'm still stuck in the OOP mindset.

In this particular case, I am trying to solve a problem using the strategy-pattern. I keep running into the problem where I end up wanting something to behave polymorphically which templates don't seem to support.

OOP Code using composition:

class Interpolator {
   public:
     Interpolator(ICacheStrategy* const c, IDataSource* const d);
     Value GetValue(const double);
}

void main(...) {
    Interpolator* i;
    if (param == 1)
       i = new Interpolator(new InMemoryStrategy(...), new TextFileDataSource(...));
    else if (param == 2)
       i = new Interpolator(new InMemoryStrategy(...), new OdbcDataSource(...));
    else if (param == 3)
       i = new Interpolator(new NoCachingStrategy(...), new RestDataSource(...));
    
    while (run) {
       double input = WaitForRequest();
       SendRequest(i->GetValue(input));
    }
}

Potential Template Version:

class Interpolator<class TCacheStrategy, class TDataSource> {
   public:
     Interpolator();
     Value GetValue(const double);               // may not be the best way but
     void ConfigCache(const& ConfigObject);      // just to illustrate Cache/DS         
     void ConfigDataSource(const& ConfigObject); // need to configured
                                                 
}

//Possible way of doing main?
void main(...) {
    if(param == 1)
       DoIt(Interpolator<InMemoryStrategy, TextFileDataSource>(), c, d);
    else if(param == 2)
       DoIt(Interpolator<InMemoryStrategy, OdbcDataSource>(), c, d)
    el
Edit
Report

1 Answer

6

Templates are sometimes called static (or compile-time) polymorphism, so yes, they can sometimes be used instead of OOP (dynamic) polymorphism. Of course, it requires the types to be determined at compile-time, rather than runtime, so it can't completely replace dynamic polymorphism.

When I often find I want to do is have something like TemplateType x = new TemplateType() where x doesn't care what X,Y is.

Yeah, that's not possible. You have to do something similar to what you have with the DoIt() function. Often, I think that ends up a cleaner solution anyway (you end up with smaller functions that do just one thing each -- usually a good thing). But if the types are only determined at runtime (as with i in the OOP version of your main function), then templates won't work.

But In this case, I think your template version solves the problem well, and is a nice solution in its own right. (Although as onebyone mentions, it does mean code gets instantiated for all three templates, which might in some cases be a problem)

answered 2009-07-31T16:30:45.903

Your Answer