Home    Personal    Work    Computers    Miscellaneous

LaTeX    Software Projects    Rendering Tutorial    UNIX Commands    Using CVS    Case Modding    Base Wiki

Convert any type to strings with streams

Include this if you want to use a templated function that converts any possible type to a string, automagically. As long as the templated class has defined a << streaming operator. Here you can find it the way I like my code styled, intended, etc. It returns a string, can't be better, a string can easily be inserted in most C++ code.

#include <string>

#include <sstream>

template <class T> std::string toString( const T & t) {

  std::ostringstream oss; // create a stream
  oss << t; // insert value to stream
  return oss.str(); // extract value and return
}

Sure, if we want a char*, then we just copy/paste the following:

#include <string>

#include <sstream>

template <class T> char* toCharPointer( const T & t) {

  std::ostringstream oss; // create a stream
  oss << t; // insert value to stream
  return oss.str().c_str(); // extract value and return
}

Edit (locked) - History - Recent Changes - Search - Statistics
All contents copyrighted 2000-2006 Anthony Liekens unless otherwise noted. Powered by PmWiki
Page last modified on October 14, 2005, at 01:45 PM.