If you were blocked in using void ptr and are absolutely necessary for calling [] with different types:
template <typename cast_to> inline int get_int_helper(someArray_t someArray, void* ptr) { return someArray[*static_cast<cast_to*>(ptr)]; } int get_int(someArray_t someArray, void* ptr, int temp) { switch ( temp ) { case 32: return get_int_helper<uint32>(someArray,ptr); case 16: return get_int_helper<uint16>(someArray,ptr); default: return get_int_helper<uint8>(someArray,ptr); } }
However, as others have pointed out; There are probably better / other ways to do this. Most likely, any array you have does not have a multiple operator [], so it doesn’t need different types. In addition, you can use boost :: variant to conduct a differentiated type union so that you do not have to go around temp
source share