Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] A non-throwing lexical cast? [Was: 5Observations - My experience with the boost libraries]
From: pete_at_[hidden]
Date: 2010-03-26 04:16:38


On Fri 26/03/10 01:25 , Gennadiy Rozental <rogeeff_at_[hidden]> wrote:

> Rene Rivera writes:
> >
> > Tom Brinkman wrote:
> > > boost::lexical_cast_no_throw()
> >
> > Or..
> >
> > template
> > T lexical_cast(const S & arg, const T & error_default_value);
> >
> I've implemented later in my own version of lexical cast. In 99% of
> the time
> this is exactly what I need: either convert string to number or take
> this
> default value. Another good consequence is that I do not need
> template parameter
> anymore.
> Gennadiy

Looking at the various threads it seems like three different behaviours in the event of conversion failure have been requested.

- Default Value
- boost::system::error_code
- throw custom exception.

So this would suggest an interface like

template< typename T , typename S, typename FailureHandler > lexical_cast(S const& arg , FailureHandler fh)

with specializations if FailureHandler = T or FailureHandler = boost::system::error_code&.

You do lose your "good consequence" with this formulation though. As I said in another message, the original author of lexical_cast has said several times over the years that doing something like this is too far from the spirit of a *_cast, though, so if it gets into Boost I think it will have to be in the StringConvert library.


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