Boost logo

Boost :

Subject: Re: [boost] [lexical-cast] version of lexical_cast in 1.46.1 is quite older than in trunk
From: Matthew Chambers (matt.chambers42_at_[hidden])
Date: 2011-03-30 11:58:19


On 3/30/2011 10:46 AM, Mika Heiskanen wrote:
> On Wed, 2011-03-30 at 18:16 +0300, Matthew Chambers wrote:
>> Does that mean lexical_cast is orphaned? I'd like to see lexical_cast have optimized conversions for
>> string->number and number->string (template specializations using strtol/strtod; or optionally using
>> the spirit-based construe_cast). Being able to pass ios flags to control the format (i.e. boolalpha)
>> would be nice too.
>
> Which reminds me - lexical_cast error messages are not very
> informative, so in order to save debugging time I've used local
> variants like this:
>
> template<typename T>
> T number_cast(const std::string& theValue)
> {
> try
> {
> return boost::lexical_cast<T>(theValue);
> }
> catch(boost::bad_lexical_cast&)
> {
> throw std::runtime_error("number_cast failed to convert
> '"+theValue+"' to " + number_name<T>());
> }
> }
>
> with
>
> template<typename T> inline const char * number_name() { return "a
> number"; }
>
> template<> inline const char * number_name<float>() { return "float"; }
> template<> inline const char * number_name<int>() { return "int"; }
> ...
>
> My template programming is not much beyond the level displayed above,
> so I will not venture to suggest how a feature like this could or should
> be incorporated into boost::lexical_cast, but I'd be very happy if
> someone would be interested and skilled enough to do so.
>
Agreed. This should be added as well. I don't care that TR1's std::lexical_cast will differ in
implementation. That's not a sound argument for letting boost::lexical_cast fester, especially when
there is so much room for improvement. In other words, how many things in boost have equivalently
named entities in std? How many of them do the exact same thing vs. do it better?

-Matt


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk