c++ - Template: get proper variable name from type name -
let's say, have 4 arrays of 4 different pointer types. , want template function, stores it's argument in proper array. this
int* arr_int[10]; double* arr_double[10]; uint64_t* arr_uint64_t[10]; template <typename t> void add_value (int pos, t value) { //i want store value in array of it's type arr_##(*t)[pos]=value; //of course, not work :) } int main(int argc, char *argv[]) { int = 2; double b = 4.2; uint64_t c = 123456; add_value(0,&a); add_value(0,&b); add_value(0,&c); //add more values //do arrays .... return 0; }
is possible?
no, name generation not possible via templates in way mean it. can nick said - create overloaded functions, or create template class static array of specified type:
template <typename t> struct array_holder { static t * arr[10]; }; template <typename t> t * array_holder<t>::arr[10]; template <typename t> void add_value (int pos, t value) { array_holder<t>::arr[pos] = something_you_need; }
Comments
Post a Comment