Note: Do not cast C style
Since you are using C++ you should avoid casting C style like (type) value
. See Why use static_cast(x) instead of (int)x? for a C++ like version.
Now let's solve your problem.
Template specialization
The idea is to specialize a template for each used generic type.
Your header could contain
template<typename T>
T ReadMemory(uint64_t addr, size_t size);
and your implementation containes
/* type is std::string */
template<>
std::string ReadMemory(uint64_t addr, size_t size){
uint64_t some_address = addr;
size_t string_size = size;
uint64_t return_value;
//store address contents into return_value using driver
//cast typename on return_value
return std::to_string(return_value);
}
/* type is uint64_t */
template<>
unit64_t ReadMemory(uint64_t addr, size_t size){
uint64_t some_address = addr;
size_t string_size = size;
uint64_t return_value;
//store address contents into return_value using driver
//cast typename on return_value
return return_value;
}
and so on for all types you want to implement. See How do I explicitly instantiate a template function? for further information.
C++17 solution
C++17 will add an amazing new feature called if constexpr
. As soon as this will be supported by compilers you can use it like this:
template<typename T>
T ReadMemory(uint64_t addr, size_t size){
uint64_t some_address = addr;
size_t string_size = size;
uint64_t return_value;
//store address contents into return_value using driver
/* type is std::string */
if constexpr (std::is_same<T,std::string>::value){
return std::to_string(return_value);
}
/* type is uint64_t */
if constexpr (std::is_same<T,uint64_t>::value){
return return_value;
}
}