Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-01-25 10:08:32


Proposal for lexical_cast/lexical_convert
>----- Original Message -----
>From: Björn Karlsson
>
>
>The reasoning below is the background for the proposal of adding a
nothrowing relative to lexical_cast, called lexical_convert.
>There are two main reasons why lexical_cast isn't always a perfect fit (as
I see it):
>0) In code that doesn't otherwise use exceptions, the error handling can
become awkward/messy/inconsistent when using lexical_cast.
>1) When a failed conversion is not considered an error, the exception
handling stands in the way of code clarity.
>

I think this would be really useful, but I would use a different signature:

   template<typename Target, typename Source>
   Target lexical_convert(Source arg, bool* succeeded = 0 ) ;

This has some advantages:

1) You don't have to remember which parameter, first or second, is the
source/target.
2) It looks closer to any other convertion function in that it returns the
converted value.
3) You can use it ignoring the boolean flag in cases you don't care to test
if it succeeded at all
    (that's why the boolean parameter is a pointer and not a reference)

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


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