Boost logo

Boost :

From: Kevlin Henney (kevlin_at_[hidden])
Date: 2005-05-01 13:56:28

In message <d755b4cc4649234beeb1cdbd730997e4_at_[hidden]>, Dave Dribin
<dave-ml_at_[hidden]> writes
>Did anyone ever get a chance to look at this feature and patch? I
>could do the documentation updates, too, if desired.
>On Apr 15, 2005, at 3:05 PM, Dave Dribin wrote:
>> Hi all,
>> I recently discovered lexical_cast, and it's very nice and elegant.
>>I'd like to put in a feature request, though: the ability to use a
>>default value rather than throwing an exception on conversion failure.
>>Basically, it would take a second argument that would be the value
>>returned if the conversion fails. This could be used directly in the
>>example, like:
>> std::vector<short> args;
>> while(*++argv)
>> {
>> args.push_back(lexical_cast<short>(*argv, 0));
>> }
>> This eliminates the throwing and catching of an exception for cases
>>where conversion failure is not an exceptional.
>> I've attached a patch to lexical_cast.hpp found in Boost 1.32.0

This issue of providing a default value in case of failure was discussed
in the early days of lexical_cast, but not included. In fact, if you go
back far enough, opting for the default or zero constructed value of the
target rather than throwing an exception was the original behaviour.

However, the accommodation of an additional argument was not included in
lexical_cast in part for the simple reason that it ceases to look like a
cast and in part because once you start down that road there are a
number of additional arguments and options that you might want that are
"reasonable". It was felt that much of this would be better off in a
general conversions library that had no pretensions to being cast-like.


   Kevlin Henney                   phone:  +44 117 942 2990
   mailto:kevlin_at_[hidden]     mobile: +44 7801 073 508        fax:    +44 870 052 2289
   Curbralan: Consultancy + Training + Development + Review

Boost list run by bdawes at, gregod at, cpdaniel at, john at