title: c++ integer->std::string conversion. Simple function?
body: <p>Problem: I have an integer; this integer needs to be converted to a stl::string type. </p>
  
  <p>In the past, I've used <code>stringstream</code> to do a conversion, and that's just kind of cumbersome. I know the C way is to do a <code>sprintf</code>, but I'd much rather do a C++ method that is typesafe(er).</p>
  
  <p>Is there a better way to do this? </p>
  
  <p>Here is the stringstream approach I have used in the past:</p>
  
  <pre><code>std::string intToString(int i)
  {
      std::stringstream ss;
      std::string s;
      ss &lt;&lt; i;
      s = ss.str();
  
      return s;
  }
  </code></pre>
  
  <p>Of course, this could be rewritten as so:</p>
  
  <pre><code>template&lt;class T&gt;
  std::string t_to_string(T i)
  {
      std::stringstream ss;
      std::string s;
      ss &lt;&lt; i;
      s = ss.str();
  
      return s;
  }
  </code></pre>
  
  <p>However, I have the notion that this is a fairly 'heavy-weight' implementation.</p>
  
  <p>Zan noted that the invocation is pretty nice, however:</p>
  
  <pre><code>std::string s = t_to_string(my_integer);
  </code></pre>
  
  <p>At any rate, a nicer way would be... nice. </p>
  
  <blockquote>
    <h3>Related:</h3>
    
    <p><a href="https://stackoverflow.com/questions/228005/alternative-to-itoa-for-converting-integer-to-string-c">Alternative to itoa() for converting integer to string C++?</a>  </p>
  </blockquote>