I have the following use case but I can't figure out how to make it work:
enum MyTemplateEnum { A, B };
template<MyTemplateEnum T = A>
class MyTemplateClass<T> {
// ...
};
std::string argument = ...;
std::auto_ptr<MyTemplateClass<> > instance;
if (argument == "a") {
std::auto_ptr<MyTemplateClass<A> > temp(new MyTemplateClass<A>(...));
instance = temp;
} else
if (argument == "b") {
std::auto_ptr<MyTemplateClass<B> > temp(new MyTemplateClass<B>(...));
instance = temp;
}
this results in compilation error because I basically can't assign a concrete realization std::auto_ptr<MyTemplateClass<A> >
to a generic version std::auto_ptr<MyTemplateClass<> >
.
You need a common base class for all instantiations of the
MyTemplateClass<T>
template. Otherwise all the instatiations are unrelated classes.Note that
std::auto_ptr
is obsolete. If possible usestd::unique_ptr
orboost::scoped_ptr
instead.