0 Members and 2 Guests are viewing this topic.
template <typename T>inline std::string tostring(const T& v){ std::stringstream s; s << v; return s.str();}
int myvariable = 150;sf::String foo( "My variable is: " + tostring(myvariable) );mywindow.Draw(foo);
#include <sstream>class MakeString{ public: template <typename T> MakeString& operator<< (const T& value) { myStream << value; return *this; } operator std::string() const { return myStream.str(); } private: std::stringstream myStream;};int main(){ std::string str = MakeString() << "The image has width " << 407 << " and height " << 588 << ".";}
Yeah that's much better than my approach.
I wouldn't say it's always better. To convert single values, your code fits quite well.
By the way, boost::lexical_cast() is similar to your tostring(), but optimized for builtin types like int, double, etc. Only for the seldom case where speed matters that much.
(with 1000 hares)