Is there a way to test a parameter's data type in an if statement? Here is an example of source code: it will not compile but it used to present my intentions.
typedef char cInt[sizeof(int)];
typedef char cFloat[sizeof(float)];
typedef char cDouble[sizeof(double)];
template<typename T>
char* convertToCharStr( const T& t ) {
if ( t == int ) {
cInt c = t;
return c;
}
if ( t == float ) {
cFloat c = t;
return c;
}
if ( t == double ) {
cDouble c = t;
return c;
}
return nullptr;
}
As you can see, I am trying to create a template function that can take any default data types such as an int, unsigned int, float, double and depending on the data type it will create the appropriate char[] variable on the stack according to the data type passed in and store the data into the char[] and return the pointer out of the function.
I will leave this above the way it is, but as a note the character arrays should of been unsigned char.