I want to throw an exception of type
throw std::domain_error("key error: "+static_cast<std::string>(key));
template <class T> std::string to_string(const T t)
template <> std::string to_string<unsigned int>(const unsigned int i)
ss << i;
ss >> ret;
std::string domain_error(const IS& is) const
using namespace IDTranslator_detail;
return "key error: "+to_string(is), "error";
It can't be done, as described, in 100% of all situations.
You must specify that a part of the contract for your template is that whatever class is passed as a parameter it must support
You could also write, as part of your contract, that numeric types would also be allowed, and you will implement this in your template, as a specialization that uses
For a robust implementation, in this situation I would use
SFINAE to try
operator std::string, and if both fail, use some bland label, like "unknown type" in the exception message. Maybe use
typeid together with my compiler's demangler to, at least, get a C++ type name out of it.