Boost logo

Boost :

Subject: Re: [boost] [lexical-cast] version of lexical_cast in 1.46.1 is quite older than in trunk
From: Mika Heiskanen (mika.heiskanen_at_[hidden])
Date: 2011-03-30 11:46:01


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.

--> Mika Heiskanen


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