Consider I have a some Factory class:
class Factory
{
public:
Factory();
virtual ~Factory();
/*
some factory methods
*/
}
All of the methods create objects according to a string given as a parameter. But one of these methods creates objects that depends on template parameter like:
template <typename Tp>
some_class<Tp>* fac_meth(string str);
The actual type will be decided at runtime.
Is it possible to do such a thing?
In one word: no.
In more words: at compile time all the instantiations of that function template will occur (i.e. the compiler will determine all the different instances of that function that is needed for different types [e.g.
some_type<int*>,some_type<double*>etc.])Consider run-time polymorphism (i.e. an inheritance hierarchy) for something like this.