#include <string>#include <sstream>template<typename R, typename T> R lexical_cast(T const& val) { R ret; std::stringstream st; if(!(st<<val) || !(st>>ret)) {}// /* No bad_cast in stlport? */ throw std::bad_cast(); return ret; }
Interger -> String
writing a lexical_class template isn''t hard. Check this out:
quote:Original post by DeyjaThere is one complication. What does this line do?
writing a lexical_class template isn''t hard. Check this out:
(snip)
std::cout <<lexical_cast<std::string>("hi mom") << std::endl;
Really? What compiler/stl implementation?
It printed a nice hex number for me. I suppose one of us has a non-standard overload for stringstream<<char*.
It printed a nice hex number for me. I suppose one of us has a non-standard overload for stringstream<<char*.
GCC 3.2 (and its supplied library implementation).
The issue which I meant to bring up was that it only reads the first token off, so casting to a string can lose information. Due to lack of compiler support for partial template specialisation, it can take a bit of trickery to get it working. The boost team can save you this trouble.
The issue which I meant to bring up was that it only reads the first token off, so casting to a string can lose information. Due to lack of compiler support for partial template specialisation, it can take a bit of trickery to get it working. The boost team can save you this trouble.
quote:Original post by Deyja
Really? What compiler/stl implementation?
It printed a nice hex number for me. I suppose one of us has a non-standard overload for stringstream<
You do. See 27.6.2.5.4 in the C++ standard.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement