I think I'm already close to the solution but am missing the final step. I want to call a templated member function where I only have a pointer to the base class and the type is already predefined, so should not depend on the actual argument type.
This is what I have:
template <typename T> class TTemplateTest;
class CTemplateTest
{
public :
CTemplateTest(){};
template <typename T> double Process(T atValue)
{
return static_cast<TTemplateTest<T>* >(this)->Process(atValue);
}
};
//------------------------------
template <class T>
class TTemplateTest:public CTemplateTest
{
public :
TTemplateTest() : CTemplateTest(){};
virtual double Process(T atNewValue) {return atNewValue;};
};
//------------------------------
template <class T>
class TTemplateTestInt:public TTemplateTest<T>
{
public :
TTemplateTestInt(){};
virtual double Process(T atNewValue);
};
//------------------------------
template <class T> double TTemplateTestInt<T>::Process(T atNewValue)
{
return atNewValue;
}
CTemplateTest* pTTest = new TTemplateTestInt<int>();
// application code
double d = 5.5;
double r;
r = pTTest->Process(d);
I would like to process the argument as integer in this example, no matter what type the argument is. It calls the right function but the value is garbage as the double is interpreted as integer instead of being converted.
I have looked at other questions (and other sites) but couldn't find a match or solution, e.g. calling a template function of a derived class
The solution probably is CRTP but I couldn't figure out how to use it. The application code should stay like this, the classes definitions can change. The reason for this code is that it is generated and used in runtime from some xml configuration file. So the type is not really known at the function call.
It would probably help if I could use the defined type, like:
template <typename T> double Process(T atValue)
{
return static_cast<TTemplateTest<T>* >(this)->Process((this::type)atValue);
}
Or prevent the automatic creation of the function using double so the argument is converted to integer as it would happen on a non templated function.
Thanks for any help.
Edit: Next solution
Does this look valid? It doesn't need casting and we only need a handful of different basic types for the template so there won't be many redirector functions. And it should still be efficient (without typeinfo and such). I'm also posting it in case somebody else has a similar problem.
class CTemplateTest
{
public :
CTemplateTest(){};
virtual inline double Process(double adValue)=0;
virtual inline double Process(int aiValue)=0;
};
//------------------------------
template <class T>
class TTemplateTest:public CTemplateTest
{
public :
TTemplateTest() : CTemplateTest(){};
virtual inline double Process(double adValue) {
return ProcessImp((T)adValue);
}
virtual inline double Process(int aiValue) {
return ProcessImpl((T)aiValue);
}
virtual double ProcessImpl(T atNewValue)=0;
};
//------------------------------
template <class T>
class CTemplateTestInt:public TTemplateTest<T>
{
public :
CTemplateTestInt(){};
virtual double ProcessImpl(T atNewValue) {return atNewValue;};
};
This then gives the desired result with
CTemplateTest* pTTest = new TTemplateTestInt<int>();
// application code
double d = 5.5;
double r;
r = pTTest->Process(d);
// -> r = 5
Thanks