I have some code which std::to_string()'s a variable whose type is a template parameter. Suppose the code is:
template <typename T>
std::string foo(const T& t) {
return std::string("I got ") + std::to_string(t);
}
Now, sometimes this parameter needs to be a void *; and I would like to get the address in the string, e.g. "I got 0xdeadbeef". No problem, right? I should get it just the same as if I did std::cout << my_ptr, right? ... Unfortunately for me, that's not the case. Well,
foo(), is there anything better to do than to overload std::to_string for void*'s (using an std::stringstream in there for the operator<<) ?namespace my_to_str {
using std::to_string;
std::string to_string(void* ptr){
// implement
}
}
Now my_to_str::to_string can be extended with your own set of overloads, like void*.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With