how to pass argv[] to template class?

Hi everyone.
I have a problem with passing program argument to template class.

program usage:
progname inputFile dataType (for example progname in1.txt double)

so argv[2] is some class name or data type (for example int, double or MyClass)

I have template class:

template <class DT>
class Pair{
public:
Pair(ifstream &in_file){in_file >> _key >> _data;}
.
.
private:
DT _data;
int _key;
};

in main I have:

int main(int argc, char *argv[])
{
.
.
.
ifstream in_file;
in_file.open(argv[1]);

char* dataType = argv[2];

Pair<dataType> pair(in_file);

and here I have error message: "'dataType' is not a valid template type argument for parameter 'DT'"
How can I solve it? i must somehow convert char* dataType(for example double) to legal class name.
A datatype must be known at compile time. Hence, you can't convert a string to a datatype at runtime. There's no way to make your design work.
so I have to use something like:

if(strcmp(argv[2],"int")==0)
typedef int dataType;
else if(strcmp(argv[2],"float")==0)
typedef float dataType;
.
.
.
Pair<dataType> pair(in_file);
???
No, you can't do that either. You can't have a conditional typedef.

You simply can't do what you want. If you want to do something like that, you may want to look at an interpreted language. You might try http://www.ruby-lang.org
Topic archived. No new replies allowed.