I'm an happy user of
Boost libraries and recently I had to mess with
QT libraries.
I didn't really like the QString-to-number conversion features of
QString class because of the clumsy error checking mechanism, so I wrote a little wrapper for
boost::lexical_cast.
Unfortunately
boost::lexical_cast is not written with extensibility in mind so a temporary
std::basic_string variable is required to perform the magic (or at least I couldn't find a better, unobtrusive way).
namespace boost { namespace detail {
#ifndef DISABLE_WIDE_CHAR_SUPPORT
inline bool operator>>(boost::detail::lexical_stream<QString, std::wstring>& in, QString& output)
{
std::wstring tmp;
in >> tmp;
output = QString::fromStdWString(tmp);
return true;
}
#endif
template<typename Source>
inline bool operator>>(boost::detail::lexical_stream<QString, Source>& in, QString& output)
{
#if defined(BOOST_NO_STRINGSTREAM)
in << '\0';
#endif
std::string tmp;
in >> tmp;
output = QString::fromStdString(tmp);
return true;
}
}}
This allows you to write something like:
QString x("123");
int id(boost::lexical_cast<int>(x));