mardi 24 mars 2015

Comparing A Data Type in an If Statement

Is there a way to test a parameter's data type in an if statement? Here is an example of source code not compliable but 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.


Aucun commentaire:

Enregistrer un commentaire