I\'ve created a small utility function for string conversion so that I don\'t have to go around creating ostringstream objects all over the place
template<
What about using type traits for serializing different types to the stream like this:
template
struct Traits {
static inline bool ToStream(std::ostringstream& o, const T& x) {
return o << x;
}
};
template
struct Traits > {
static inline bool ToStream(std::ostringstream& o, const std::pair& x) {
return o << "[" << x.first << "," << x.second << "]";
}
};
template
inline std::string ToString(const T& x)
{
std::ostringstream o;
if (!Traits::ToStream(o, x))
return "Error";
return o.str();
}
Note: "template<>" from the specialization part is optional, the code compiles fine without it. You can further add methods in the traits for the exception message.