Re: alternative for virtual template function?
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