
I have tried to use lexical_cast many times but I always run into problems sooner or later due to the lack of locale support. Why not add a locale overload (or default argument) to lexical_cast. std::string x = lexical_cast<std::string>(1234); // "1 234" on my PC std::string y = lexical_cast<std::string>(1234, std::locale::classic ()); // "1234" on my PC double xmlDbl1 = lexcal_cast<double>("1.23"); // bad_cast on my PC double xmlDbl2 = lexcal_cast<double>("1.23", std::locale::classic()); The change would be minimal lexical_stream() { becomes lexical_stream(const std::locale& loc) { stream.imbue(loc); and template<typename Target, typename Source> Target lexical_cast(Source arg) { detail::lexical_stream<Target, Source> interpreter; becomes template<typename Target, typename Source> Target lexical_cast(Source arg, const std::locale& loc = std::locale()) { detail::lexical_stream<Target, Source> interpreter(loc);

From: "Martin" <adrianm@touchdown.se>
I have tried to use lexical_cast many times but I always run into problems sooner or later due to the lack of locale support.
Why not add a locale overload (or default argument) to lexical_cast.
std::string x = lexical_cast<std::string>(1234); // "1 234" on my PC std::string y = lexical_cast<std::string>(1234, std::locale::classic ()); // "1234" on my PC
double xmlDbl1 = lexcal_cast<double>("1.23"); // bad_cast on my PC double xmlDbl2 = lexcal_cast<double>("1.23", std::locale::classic());
This issue has come up with quite regular intervals. The current docs says a little bit about this: "Where a higher degree of control is required over conversions, std::stringstream and std::wstringstream offer a more appropriate path." However, an earlier version of the docs contained more information on this(http://cvs.sourceforge.net/viewcvs.py/*checkout*/boost/boost/libs/conve rsion/lexical_cast.htm?content-type=text%2Fplain&rev=1.4): --- Start quote --- Future directions - It is also worth mentioning future non-directions: anything that involves adding extra arguments for a conversion operation is not being considered. A custom keyword cast, such as lexical_cast, is intended to look like a built-in cast operator: built-in cast operators take only a single operand. Where a higher degree of control is required over conversions, the standard stringstream offers a more appropriate path. Where non-stream-based conversions are required, lexical_cast is the wrong tool for the job, and so it won't be special-cased for such scenarios. --- End quote --- (Kevlin Henney isn't following the Boost list (at least, he didn't use to).) Regards, Terje
participants (2)
-
Martin
-
Terje Slettebø