It is possible to choose template parameter in runtime?
when i try to construct an object wich has template parameter

No but you can adjust I guess.

string type;
cout<<"Choose Type  (int,string,float) : ";
cin >> type;

if(type == "string"){
myClass<string> stringClass;
//do stuff
}
else if(type == "float"){
myClass<float> floatClass;
//do stuff
}
//and so on.

But C++ does not runtime templates.

It is not possible to choose the same name stringClass = floatClass not to repeat the whole code twice?

Edited 6 Years Ago by merse: n/a

Of course dude!! The following code should help...

//This program finds the maximum of two inputs.
//However, the data type for the inputs is decided by the user.. at runtime!!
#include<iostream>
using namespace std;
 template<class T>
 T maximum(T& var1,T& var2){
 if(var1>var2){
 cout<<var1<<" is the greatest!!"<<endl;
 return var1;
 }
 else{
 cout<<var2<<" is the greatest!!"<<endl;
 return var2;
 }
 }
 int check_class(string type){
 if(type=="int")
 return 1;
 if(type=="float")
 return 2;
 if(type=="char")
 return 3;
 if (type=="string")
 return 4;
 }

 int main()
{
 cout<<"I have a program that finds the maximum of any two inputs.."<<endl;
 cout<<"Do you want to continue?(y/n)";
 char yn;
 cin>>yn;
 yn=toupper(yn);
 while(yn=='Y'){
 cout<<endl<<"What data type do ypou want to use?"<<endl<<"(Since this is the beta version of this program, it can only support ints,floats,chars and strings only)";
 string response;
 cin>>response;
 int num=check_class(response);
 switch(num){
 case 1:{//int
 int one=0,two=0;
 cout<<"Enter two numbers..";
 cin>>one;
 cout<<endl;cin>>two;
 maximum<int>(one,two);
 }
 break;
 case 2:{//float
 float one=0.0,two=0.0;
 cout<<"Enter two float numbers..";
 cin>>one;cout<<endl;cin>>two;
 maximum<float>(one,two);
 }
 break;
 case 3:{//char
 char one,two;
 cout<<"Enter two chars..";
 cin>>one;cout<<endl;cin>>two;
 maximum<char>(one,two);
 }
 break;
 case 4:{//string
 string one,two;
 cout<<"Enter two words..";
 cin>>one;cout<<endl;cin>>two;
 maximum<string>(one,two);
 }
 break;
 default:
 cout<<"This program does not support this data type!!"<<endl;
 break;
 }
 cout<<"Do you want to continue?(y/n)";
 cin>>yn;
 yn=toupper(yn);
 }
 cout<<"Thank you for using this program!!"<<endl;
 return 0;
}

Apart from allowing the user to chhose template params at runtime, it also allows a programmer to update it e.g you might want to add a class of your own as a datatype, all you have to do is to update the check_class() function..

if(type=="myclass")//myclass is the nameof your own class
return 5;

I hope this satisfies your question..

Edited 6 Years Ago by tkud: n/a

Thank you for your answer.
So you have to write out always maximum<char> or maximum<int> etc.
There is no solution like maximum<var> or maximum<gettype(...)> something like that? between the <>brackets you alway have to write a single typename?

My other question is:

I have a class with template parameter.
I try to write something like this:

if (sizef var == sizeof(double))
{
 ...
}
else 
// var is a user defined class having a member function get()
{
  var.get();
}

But compiler throw an error, becaue of using get() since I call this with a double.

Why exactly do you need the runtime template parameter ?

And check your spelling for sizeof.

Edited 6 Years Ago by firstPerson: n/a

In your example you have a function template: maximum<>,
but what I have is a template class myclass<>!
And my problem is that the classes have different member functions
and I cannot use them!
For example my template parameter can be (I would like to be)
double or my wn defined complex class.
But I can not use member function of my complex class,
because double dont have them,
since in runtime I dont call it with double.

In your example you have a function template: maximum<>,
but what I have is a template class myclass<>!
And my problem is that the classes have different member functions
and I cannot use them!
For example my template parameter can be (I would like to be)
double or my wn defined complex class.
But I can not use member function of my complex class,
because double dont have them,
since in runtime I dont call it with double.

Can you post a code of your problem or explain a bit more clearer?

OK lets see an example:

class mystruct
{
  double x, y;
public:
  mystruct() : x(0), y(0) {}
  double multiply() { return x*y; } 
};

template <class T>
class myclass
{
  T z;
  mystruct s;
public:
  void function()
  {
    if (sizeof(z) == sizeof(s)) z.multiply();
  }
};

int main ()
{
  myclass<double> myobj1;
  myclass<mystruct> myobj2;
  //myobj1.function();
  myobj2.function();
  return 0;
}

This call:

myobj1.function();

does not work because double doesn't have member function 'multiply' however the code have not to go to that point!

So my question is:
if I have a template class
and the template parameter can be for example A and B,
but B do not have the member function C what A have,
than I cannot use C (althoug I check the type of the template parameter is not equal to B)?

Then you can either make multiply a template function. Or make mystruct
a template class with myclass begin a template class that takes 2
typenames argument.

This article has been dead for over six months. Start a new discussion instead.