We Recommend

C++ The Core Language C++ The Core Language
C++: The Core Language is for C programmers transitioning to C++. It's designed to get readers up to speed quickly by covering an essential subset of the language. The subset consists of features without which it's just not C++, and a handful of others that make it a reasonably useful language.


Posted By

yuconner on 10/05/06


Tagged

strings templates


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

copyleft
jeffhung


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.