The title says it all really. Why did they choose to not have a
namespace std
{
std::string to_string(const std::string&)
}
overload?
I have an program that can query some data by row index or row name; ideal for templates. Until I try to construct an error message if a row being accessed is missing:
template <typename T>
int read(T nameOrIndex)
{
if (!present(nameOrIndex))
{
// This does not compile if T is std::string.
throw std::invalid_argument("Missing row: " + std::to_string(nameOrIndex));
}
}
I could add my own overload to the std namespace but that's not ideal.
Tis any number of other types too. What about all of those? Should there be ato_stringimplementation that returns some string for arbitrary types? Instead of addingto_stringoverloads tostd(which is illegal), I'd create my ownto_stringoverloads in the same namespace asread. Delegate tostd::to_stringfrom that, and return the argument as is from the overload that takesstd::string const&.std::to_stringconverts built-in numeric types to strings. There's no corresponding need to "convert" strings to strings because they already are strings.to_stringis basically a safe interface to the C formatting utilities that conceals the buffer and all that from you. There is a C formatting specifier%sfor strings. Thus, something has clearly been lost :)int x; int y = static_cast<int>(x);.