"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c7**********@chessie.cirr.com...
Tim Love <tp*@eng.cam.ac.uk> spoke thus:
See the FAQ - e.g.
http://www.parashift.com/c++-faq-lit...al-issues.html
Oops!!! I should have known better - sorry for not reading the holy
writ first :( And unfortunately, I can't use the C++ solution
either, since stringstreams are hit or miss on my (horribly broken)
system. Oh well.
I have two suggestions:
1. See if you can use lexical_cast from the Boost library:
http://www.boost.org/libs/conversion/lexical_cast.htm
It's an elegant solution, and it looks like the author made an attempt to
work around broken C++ implementations.
2. I've rewritten the code in the FAQ to use the deprecated strstream
classes instead of the stringstream classes. Maybe it will work for you:
#ifndef CONVERT_H
#define CONVERT_H
// File: convert.h
// Based on code from the C++ FAQ Lite:
//
http://www.parashift.com/c++-faq-lit....html#faq-38.3
// Modified to use strstream classes instead of stringstream classes.
#include <istream>
#include <ostream>
#include <stdexcept>
#include <string>
#include <strstream>
#include <typeinfo>
class BadConversion : public std::runtime_error {
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};
template<typename T>
inline std::string stringify(const T& x)
{
std::ostrstream o;
if (!(o << x << std::ends))
throw BadConversion(std::string("stringify(")
+ typeid(x).name() + ")");
std::string s(o.str());
o.freeze(false);
return s;
}
template<typename T>
inline void convert(const std::string& s, T& x,
bool failIfLeftoverChars = true)
{
std::istrstream i(s.c_str());
char c;
if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
throw BadConversion(s);
}
template<typename T>
inline T convertTo(const std::string& s,
bool failIfLeftoverChars = true)
{
T x;
convert(s, x, failIfLeftoverChars);
return x;
}
#endif
--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.