However, when instantiating a templated object you still must specify the type. E.g.
DECLARATION:
template<typename T>
struct point {
T x T y;};
INSTANTIATION:
point<int> p; // implicit instance of point
I know you can use the C++ typeid operator to extract a string containing the type name of an object. E.g
Obj X; cout << "object X is of type" << typeid(X).name();
Is it possible to use this operator to dynamically instantiate an object of the appropriate type? i.e.
point<typeid(X).name()> p; // instantiate instance of point with appropriate data type
If not, is there another way to achieve this assuming we are only talking about basic types (not user defined types).
Thus far the only way I have seen this done is via a series of if/else typeid comparisons for each type you want to handle:
ObjX; int i, char c, float f; if ( typeid (i)==typied (X)) point<int> p; else if ( typeid (c)==typied (X)) point<char> p; else if ( typeid (f)==typied (X)) point<float> p;
I was hoping to avoid having to maintain a hardcoded list of such comparisons if possible.
Thanks












