Re: [Boost-Users] lexical_cast bug.
From: "Eduardo Chao"
I am a new suscriber to the list. I want to report what I consider a bug in lexical_cast conversion template. First, I wanted to use it to convert things to std::string (Target). It throws a bad_lexical_cast_exception when I pass an empty std::string object (one that is default-constructed or constructed with "") as the Source argument. It throws it because the condition (interpreter >> result ) evaluates to false.
Yes, this is a bug in the current version, see (http://www.boost.org/libs/conversion/lexical_cast.htm#future). There's a proposition which fixes this and other problems here (http://groups.yahoo.com/group/boost/files/lexical_cast_proposition/lexical_ cast_proposition.zip). It's scheduled to be reviewed by the author, Kevlin Henney, or Björn Karlsson (Boost's maintenance wizard), before Boost release 1.30. If they find it ok, a notice will also be posted at the Boost list, so that others may give feedback before the release, as well.
My solution was to specialize the function template this way:
template<> lexical_caststd::string(const std::string &str) { return str; }
but it is not so general to support const std::string or const std::string& as Target type or perhaps std::string or const std::string etc as Source. Correct me if I am wrong, so I learn from you more experienced Boost (and
The proposition handles it in a similar way. However, it's not possible to
partially specialise function templates (e.g. to handle all T ->
std::basic_string conversions), so function template overloading is used,
instead. This is because it needs to handle conversion from any type to
std::basic_string, as there may be other types which output empty strings or
whitespace, as well.
then C++) users.
No, you're right. However, it's only necessary to handle T ->
std::basic_string (not cv-qualification or reference) in this case, because:
lexical_caststd::string(...); // Ok
lexical_cast<const std::string>(...); // Error, can't write to a const
string
lexical_cast
participants (1)
-
Terje Slettebø