|
Boost : |
Subject: Re: [boost] [lexical_cast] A suggestion
From: Vladimir Batov (batov_at_[hidden])
Date: 2009-01-14 00:41:25
> ...
> This doesn't look like a good interface due to using a casted value as
> a "null"-code. This code would fail when "-1" is a value of
> some-string. This could lead to very big problems!
> I would suggest something more like
> boost::optional<int> value =
> boost::lexical_cast(some-string,boost::no_throw);
> if(!value) conversion failed.
Well, "very big problems" sounds like a stretch. I've been using a
lexical_cast equivalent in that fashion and in practice very often there are
values that are well out our sensible range. If, say, -1 is no good then
something like INT_MAX might well be. Quite often though I do not even need
to look for a ""null"-code" as I provide the *default* value instead. I.e. a
value inside the sensible range that I proceed with when the conversion
fails. If all that is no good, then I'd expect the existing interface to
come to the rescue.
In addition, the interface above does not seem to address #2, i.e. the
default-constructability requirement of the Target which for my a big
hassle.
Best,
V.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk