toStr – A small C++ utility function.

This can be used as string(“bob”) + toStr(5) without declaring the type, presuming that the type can correctly be inferred by the compiler. Obviously, T must support operator<<.

template <class T>
static inline std::string toStr(T v)
  std::stringstream s;
  s << v;
  return s.str();

Leave a Reply

Your email address will not be published. Required fields are marked *