This question already has an answer here:
- C++11 type to enum mapping? 3 answers
I'm trying with a function template call in which one of the template parameters is a enumerated type variable that needs to be resolved to a primitive data type. I couldn't get rid of enum because of other considerations.
USER_FLOAT --> float
, USER_DOUBLE --> double
I have tried with alias, typedef, using directives for the enum type variables but could find a solution. I am trying to avoid the crude way of checking the template parameter T
with in the function to_double_or_float
.
For e.g:
template<typename T, typename X>
void
to_double_or_float(void* in_arr, size_t SIZE, size_t arr_index, X* out_arr)
{
if( T == USER_FLOAT )
float* tmp_in_arr = static_cast<float*>(in_arr);
else if(T == USER_DOUBLE )
double* tmp_in_arr = static_cast<double*>(in_arr);
.....
}
#include <iostream>
#include <vector>
enum NcompTypes
{
USER_FLOAT, // needs to resolve to float
USER_DOUBLE, // needs to resolve to double
USER_INT // need to resolve to int
};
template<typename T, typename X>
void
to_double_or_float(void* in_arr, size_t SIZE, size_t arr_index, X* out_arr)
{
T* tmp_in_arr = static_cast<T*>(in_arr);
for(size_t i = 0; i < SIZE; i++)
{
out_arr[i] = static_cast<X>(tmp_in_arr[i + arr_index]);
}
return;
}
int main(void)
{
float inArr[] = {1.12, 2, 3, 4, 5.23, 6, 7, 8, 34, 43}; // input
std::vector<double> outArr(10); // output
to_double_or_float<USER_FLOAT, double>(inArr, 10, 0, outArr.data());
}
Would be great, if any one can provide some pointers on this.
Thanks !
Aucun commentaire:
Enregistrer un commentaire