Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using typeid to check for template type

I would like to know if doing the following is safe:

template<class T>
void Parameters::add(Parameter<T> p)
{
   std::string sprobe("");
   int iprobe = 0;
   double dprobe = 0.;

   if (typeid(T) == typeid(sprobe))
     this->mstrings[p.name()] = p;

   if (typeid(T) == typeid(iprobe))
     this->mints[p.name()] = p;

   if (typeid(T) == typeid(dprobe))
     this->mdoubles[p.name()] = p;
}

I have a Class for storing parameters. It has 3 boost::unordered_map member variables for storing parameters of type int, double and std::string;

I created a template class Parameter.

I understand that if my Parameter is not one of the 3 types that I anticipated this will fail. But it is not a problem since I know that the Parameters can only be of these types.

Thanks for your help

like image 591
tvery42 Avatar asked Oct 23 '25 01:10

tvery42


1 Answers

The code won't compile, but not because of typeid. The problem is that even with the correct if-clauses, the code of your method needs to be compiled - all of it. That is independent of whether or not a part of the code is executed (=evaluated) or not. This leads to the problem that if T is int, you still need to be able to compile the code for the other cases, e.g., this line:

this->mstrings[p.name()] = p;

The type of mstrings is very likely incompatible with passing Parameter<int> as p, hence you will get a compile error.

The solution is to use overloading where each method must only compile one case, but not the others, example for int:

void Parameters::add(Parameter<int> p)
{
    this->mints[p.name()] = p;
}

and likewise for the other cases.

Final note: Even if you use typeid, you don't need the probes. You can simply use typeid(int) directly.

like image 115
Daniel Frey Avatar answered Oct 25 '25 14:10

Daniel Frey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!