Posted By

yuconner on 10/05/06


Tagged

strings templates


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

copyleft
jeffhung
khouser


template to string


 / Published in: C++
 

  1. #include <string>
  2. #include <sstream>
  3.  
  4. template <class T> std::string TToStr( const T &t )
  5. {
  6. std::ostringstream oss;
  7. oss << t;
  8. return std::string (oss.str());
  9. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jeffhung on January 24, 2008

Why create an extra temporary std::string instance when returning? Isn't the following good enough?

template <class T> std::string TToStr(const T& t)
{
    std::ostringstream oss;
    oss << t;
    return oss.str();
}

You need to login to post a comment.