Can typeid (or some other way to dynamically pass the type) be used to invoke a templated function.
Ultimately I need a conversion function which will convert data buffers from about a dozen source types to a dozen destination types which leads to hundred cases to statically code. It would be nice to be able to pass type information for source and destination which would automatically build the appropriate template function and invoke it.
Here is the simplified code that demonstrates what I am trying to do:
template<typename T>
void myFunc(T buf, int val) { *buf = val; }
const std::type_info& GetTypeInfo(int csType)
{
switch(csType)
{
case dsCHAR:
{
return typeid(char*);
}
case dsWCHAR:
{
return typeid(wchar_t*);
}
default:
{
return typeid(int*);
}
}
}
void convert(void* buf, int csType, char* src, int len)
{
const std::type_info& theType = GetTypeInfo(csType);
for(int ix = 1; ix < len; ix++)
{
myFunc<theType>(&dynamic_cast<theType>(buf)[ix], src[ix]); // <- This fails to compile
}
}
Using type_info&
with the template or in a cast is not allowed by the compiler and I have not been able to figure out how to get around it, if it is even possible.