Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] A non-throwing lexical cast? [Was: 5 Observations - My experience with the boost libraries]
From: Bruno Santos (bsantos_at_[hidden])
Date: 2010-03-25 14:17:05


Hi,

Rene Rivera wrote:
> Kim Barrett wrote:
> > On Mar 24, 2010, at 5:20 PM, Bruno Santos wrote:
> >> Qua, 2010-03-24 às 09:31 +0000, pete_at_[hidden] escreveu:
> >>> Taking an example from way back at the start othis thread, in my
> >>> personal experience a non-throwing version of lexical_cast<>
> >>> would be very nice.
> >>>
> >>> If I look at the cases where I pass user data through my
> >>> libraries into boost libraries without some pre-validation, there
> >>> are very few, and naturally these are the cases where the
> >>> validation would as difficult as the "parse":
> >>>
> >>> - boost::spirit::phrase_parse - Just returns a true/false, and I
> >>> build a nice context-driven error message myself if necessary. -
> >>> boost::lexical_cast - throws. Almost always needs to be caught
> >>> and re-thrown.
> >> Why? Is it because you need to do some cleanup? Do you known what
> >> RAII is?
> >
> > Exception translation, i.e. "re-thrown" as in "thrown in a different
> > form". boost::bad_lexical_cast is not derived from
> > my_project::invalid_user_input o or whatever. This happens a lot with
> > boost::lexical_cast.
>

What about boost::system::error_code as the second parameter for the non
throwing version? And it does not suffer from the return error code
problem. Just take a look at boost::filesystem and boost::asio.

>
> So a possible improvement to the lexical_cast design would be to have an
> optional template arg specifying the exception to throw for errors? Or
> perhaps some other general/global way of lexical_cast looking up an
> exception to throw instead of the ones it does?
>
>


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