alternative for virtual template function?

Discussion in 'C++' started by Markus Dehmann, Jul 23, 2008.

  1. I have an abstract base class which contains a function that I would
    like to template, but virtual template functions are illegal. I put a
    mini code example below, which doesn't do anything great, but reflects
    the structure of my problem.

    In a nutshell, the abstr. base class VectorCreator has a function
    createVector whose return type is unfortunately fixed to
    vector<double>. Only now I discovered that some implementations would
    like to work with other types, such as vector<int>. I could just
    template the VectorCreator class, but the problem is that pointers to
    VectorCreator are passed around in the whole project, so templating it
    would require too many changes. It's okay to change the createVector
    function, but not the whole VectorCreator class (by introducing a
    class-scope template). Here is the example code:

    #include <iostream>
    #include <vector>

    // ABC
    class VectorCreator {
    public:
    virtual ~VectorCreator() {}
    virtual std::vector<double>* createVector() = 0; // it's illegal to
    template the function :(
    };

    // concrete implementation example
    class VectorCreator1 : public VectorCreator {
    typedef std::vector<double> T;
    public:
    VectorCreator1() {}
    ~VectorCreator1() {}
    T* createVector() {
    T* type = new T();
    type->push_back(10.2);
    return type;
    }
    };

    // like foo here, there are many fct's and classes that are called or
    // initialized with VectorCreator*, so VectorCreator cannot be
    // templated (would require changing hundreds of source files)
    void foo(VectorCreator* sc){
    std::vector<double>* myvector = sc->createVector();
    std::cout << (*myvector)[0] << std::endl;
    delete myvector;
    }

    int main(double argc, char** argv){
    VectorCreator* sc = new VectorCreator1();
    foo(sc);
    delete sc;
    return EXIT_SUCCESS;
    }

    I think I found an acceptable solution (see below), but I was
    wondering if anyone has a better idea how to deal with this problem. I
    heard that the Visitor pattern might help in situations where one
    would like virtual templates, but I don't know how.


    #include <iostream>
    #include <vector>

    // ABC
    class VectorCreator {
    public:
    virtual ~VectorCreator() {}
    virtual void createVector(std::vector<double>*&) = 0;
    virtual void createVector(std::vector<int>*&) = 0;
    };

    // concrete class example
    class DoubleVectorCreator : public VectorCreator {
    public:
    DoubleVectorCreator() {}
    ~DoubleVectorCreator() {}
    void createVector(std::vector<double>*& x) {
    x = new std::vector<double>();
    x->push_back(10.2);
    }
    void createVector(std::vector<int>*& x) {
    std::cerr << "unimplemented";
    }
    };

    class IntVectorCreator : public VectorCreator {
    public:
    IntVectorCreator() {}
    ~IntVectorCreator() {}
    void createVector(std::vector<int>*& x) {
    x = new std::vector<int>();
    x->push_back(10);
    }
    void createVector(std::vector<double>*& x) {
    std::cerr << "unimplemented";
    }
    };

    void foo(VectorCreator* sc){
    // specialized behaviors
    if(dynamic_cast<DoubleVectorCreator*>(sc)){
    std::vector<double>* myvector;
    sc->createVector(myvector);
    std::cout << (*myvector)[0] << std::endl;
    delete myvector;
    }
    else if(dynamic_cast<IntVectorCreator*>(sc)){
    std::vector<int>* myvector;
    sc->createVector(myvector);
    std::cout << (*myvector)[0] << std::endl;
    delete myvector;
    }
    }

    int main(double argc, char** argv){
    VectorCreator* sc = new DoubleVectorCreator();
    foo(sc);
    delete sc;

    VectorCreator* sc2 = new IntVectorCreator();
    foo(sc2);
    delete sc2;

    return EXIT_SUCCESS;
    }

    Thanks!
    Markus
    Markus Dehmann, Jul 23, 2008
    #1
    1. Advertising

  2. Markus Dehmann

    mud209

    Joined:
    Jul 21, 2008
    Messages:
    2
    Virtual template functions

    I don't think Visitor will help. This pattern helps you add new virtual functions to a class hierarchy, rather than do the kind of inheritance thing you're trying to do.

    Simplest solution? Rename VectorCreator to VectorCreatorBase and template it as you want. Then typedef VectorCreator to VectorCreatorBase<std::vector<double> >.

    Might not be the cleanest, but would work well enough.
    mud209, Jul 23, 2008
    #2
    1. Advertising

  3. Markus Dehmann

    martinb Guest

    > I have an abstract base class which contains a function that I would
    > like to template, but virtual template functions are illegal. I put a
    > mini code example below, which doesn't do anything great, but reflects
    > the structure of my problem.
    >
    > In a nutshell, the abstr. base class VectorCreator has a function
    > createVector whose return type is unfortunately fixed to
    > vector<double>. Only now I discovered that some implementations would
    > like to work with other types, such as vector<int>. I could just
    > template the VectorCreator class, but the problem is that pointers to
    > VectorCreator are passed around in the whole project, so templating it
    > would require too many changes. It's okay to change the createVector
    > function, but not the whole VectorCreator class (by introducing a
    > class-scope template).


    I think you'd better template the class. The problem with old code can
    be
    circumventÅd by introducing a new class template and typdef-ing it:

    template<typename T>
    class VectorCreatorTempl {
    public:
    virtual ~VectorCreator() {}
    virtual std::vector<T>* createVector() = 0;
    };

    typedef VectorCreatorTempl<double> VectorCreator;


    BTW: For factory functions it is generally preferred to return an
    auto_ptr
    instead of a raw pointer.
    martinb, Jul 23, 2008
    #3
  4. Markus Dehmann

    Leandro Melo Guest

    On 23 jul, 02:48, Markus Dehmann <> wrote:
    > I have an abstract base class which contains a function that I would
    > like to template, but virtual template functions are illegal. I put a
    > mini code example below, which doesn't do anything great, but reflects
    > the structure of my problem.
    >
    > In a nutshell, the abstr. base class VectorCreator has a function
    > createVector whose return type is unfortunately fixed to
    > vector<double>. Only now I discovered that some implementations would
    > like to work with other types, such as vector<int>. I could just
    > template the VectorCreator class, but the problem is that pointers to
    > VectorCreator are passed around in the whole project, so templating it
    > would require too many changes. It's okay to change the createVector
    > function, but not the whole VectorCreator class (by introducing a
    > class-scope template). Here is the example code:
    >
    > #include <iostream>
    > #include <vector>
    >
    > // ABC
    > class VectorCreator {
    > public:
    >   virtual ~VectorCreator() {}
    >   virtual std::vector<double>* createVector() = 0; // it's illegal to
    > template the function :(
    >
    > };
    >
    > // concrete implementation example
    > class VectorCreator1 : public VectorCreator {
    >   typedef std::vector<double> T;
    > public:
    >   VectorCreator1() {}
    >   ~VectorCreator1() {}
    >   T* createVector() {
    >     T* type = new T();
    >     type->push_back(10.2);
    >     return type;
    >   }
    >
    > };
    >
    > // like foo here, there are many fct's and classes that are called or
    > // initialized with VectorCreator*, so VectorCreator cannot be
    > // templated (would require changing hundreds of source files)
    > void foo(VectorCreator* sc){
    >   std::vector<double>* myvector = sc->createVector();
    >   std::cout << (*myvector)[0] << std::endl;
    >   delete myvector;
    >
    > }
    >
    > int main(double argc, char** argv){
    >   VectorCreator* sc = new VectorCreator1();
    >   foo(sc);
    >   delete sc;
    >   return EXIT_SUCCESS;
    >
    > }
    >
    > I think I found an acceptable solution (see below), but I was
    > wondering if anyone has a better idea how to deal with this problem. I
    > heard that the Visitor pattern might help in situations where one
    > would like virtual templates, but I don't know how.
    >
    > #include <iostream>
    > #include <vector>
    >
    > // ABC
    > class VectorCreator {
    > public:
    >   virtual ~VectorCreator() {}
    >   virtual void createVector(std::vector<double>*&) = 0;
    >   virtual void createVector(std::vector<int>*&) = 0;
    >
    > };
    >
    > // concrete class example
    > class DoubleVectorCreator : public VectorCreator {
    > public:
    >   DoubleVectorCreator() {}
    >   ~DoubleVectorCreator() {}
    >   void createVector(std::vector<double>*& x) {
    >     x = new std::vector<double>();
    >     x->push_back(10.2);
    >   }
    >   void createVector(std::vector<int>*& x) {
    >     std::cerr << "unimplemented";
    >   }
    >
    > };
    >
    > class IntVectorCreator : public VectorCreator {
    > public:
    >   IntVectorCreator() {}
    >   ~IntVectorCreator() {}
    >   void createVector(std::vector<int>*& x) {
    >     x = new std::vector<int>();
    >     x->push_back(10);
    >   }
    >   void createVector(std::vector<double>*& x) {
    >     std::cerr << "unimplemented";
    >   }
    >
    > };
    >
    > void foo(VectorCreator* sc){
    >   // specialized behaviors
    >   if(dynamic_cast<DoubleVectorCreator*>(sc)){
    >     std::vector<double>* myvector;
    >     sc->createVector(myvector);
    >     std::cout << (*myvector)[0] << std::endl;
    >     delete myvector;
    >   }
    >   else if(dynamic_cast<IntVectorCreator*>(sc)){
    >     std::vector<int>* myvector;
    >     sc->createVector(myvector);
    >     std::cout << (*myvector)[0] << std::endl;
    >     delete myvector;
    >   }
    >
    > }
    >
    > int main(double argc, char** argv){
    >   VectorCreator* sc = new DoubleVectorCreator();
    >   foo(sc);
    >   delete sc;
    >
    >   VectorCreator* sc2 = new IntVectorCreator();
    >   foo(sc2);
    >   delete sc2;
    >
    >   return EXIT_SUCCESS;
    >
    > }



    I'd template the class or the function and refactor the code. If
    you're going to start using dynamic_cast across all of your code, you
    don't even need the createVector() function as a virtual in the base
    class. Just dynamic cast to the appropriate pointer and call a
    function such as createIntVector() or createDoubleVector() which you
    can create in the derived classes.

    --
    Leandro T. C. Melo
    Leandro Melo, Jul 24, 2008
    #4
  5. Markus Dehmann

    Calum Grant Guest

    Markus Dehmann wrote:
    > I have an abstract base class which contains a function that I would
    > like to template, but virtual template functions are illegal. I put a
    > mini code example below, which doesn't do anything great, but reflects
    > the structure of my problem.
    >
    > In a nutshell, the abstr. base class VectorCreator has a function
    > createVector whose return type is unfortunately fixed to
    > vector<double>. Only now I discovered that some implementations would
    > like to work with other types, such as vector<int>. I could just
    > template the VectorCreator class, but the problem is that pointers to
    > VectorCreator are passed around in the whole project, so templating it
    > would require too many changes. It's okay to change the createVector
    > function, but not the whole VectorCreator class (by introducing a
    > class-scope template). Here is the example code:
    >


    <snip>

    > #include <iostream>
    > #include <vector>
    >
    > // ABC
    > class VectorCreator {
    > public:
    > virtual ~VectorCreator() {}
    > virtual void createVector(std::vector<double>*&) = 0;
    > virtual void createVector(std::vector<int>*&) = 0;
    > };
    >
    > // concrete class example
    > class DoubleVectorCreator : public VectorCreator {
    > public:
    > DoubleVectorCreator() {}
    > ~DoubleVectorCreator() {}
    > void createVector(std::vector<double>*& x) {
    > x = new std::vector<double>();
    > x->push_back(10.2);
    > }
    > void createVector(std::vector<int>*& x) {
    > std::cerr << "unimplemented";
    > }
    > };
    >
    > class IntVectorCreator : public VectorCreator {
    > public:
    > IntVectorCreator() {}
    > ~IntVectorCreator() {}
    > void createVector(std::vector<int>*& x) {
    > x = new std::vector<int>();
    > x->push_back(10);
    > }
    > void createVector(std::vector<double>*& x) {
    > std::cerr << "unimplemented";
    > }
    > };
    >
    > void foo(VectorCreator* sc){
    > // specialized behaviors
    > if(dynamic_cast<DoubleVectorCreator*>(sc)){
    > std::vector<double>* myvector;
    > sc->createVector(myvector);
    > std::cout << (*myvector)[0] << std::endl;
    > delete myvector;
    > }
    > else if(dynamic_cast<IntVectorCreator*>(sc)){
    > std::vector<int>* myvector;
    > sc->createVector(myvector);
    > std::cout << (*myvector)[0] << std::endl;
    > delete myvector;
    > }
    > }
    >
    > int main(double argc, char** argv){
    > VectorCreator* sc = new DoubleVectorCreator();
    > foo(sc);
    > delete sc;
    >
    > VectorCreator* sc2 = new IntVectorCreator();
    > foo(sc2);
    > delete sc2;
    >
    > return EXIT_SUCCESS;
    > }
    >


    Notice that the foo() function is switching on type. This is a classic
    anti-pattern, which is resolved by implementing foo() in the
    VectorCreator class itself.

    i.e.

    void DoubleVectorCreator::foo()
    void IntVectorCreator::foo()

    (There are other ways of switching on type too, such as function
    overloading and templates.)

    The question is what you need to *do* with these vectors/factories, that
    requires them to have the same base class? What's the common
    functionality? If they are logically separate then the only moral thing
    to do is to separate them in your code.

    The 'std::cerr<<"unimplemented"' line is another OO anti-pattern. It's
    normal for factories to create objects with a common base class (e.g.
    my_vector):

    std::auto_ptr<my_vector> IntVectorCreator::create()
    std::auto_ptr<my_vector> DoubleVectorCreator::create()

    If you don't like dynamic memory allocation and virtual functions, then
    try templates.

    Hope that helps a little,

    Calum
    Calum Grant, Jul 25, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    2
    Views:
    406
    Jack Klein
    Jun 21, 2004
  2. IK
    Replies:
    2
    Views:
    594
    hemraj
    Jul 23, 2004
  3. Replies:
    4
    Views:
    365
    mlimber
    Jan 23, 2006
  4. Peng Yu
    Replies:
    3
    Views:
    756
    Thomas J. Gritzan
    Oct 26, 2008
  5. GeeRay
    Replies:
    12
    Views:
    1,053
Loading...

Share This Page