Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] A suggestion
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2009-02-06 12:36:34

Alexander Nasonov wrote:
> Andrew Troschinetz <ast <at>> writes:
>> Here's some example usage:
> It's not clear from the example that this inliner
> lexical_cast< optional<int> >("x").get_value_or(-1);
> is a replacement for
> lexical_cast("x", -1);
> It's not conceptually correct but we already have an exception for
> handling spaces in strings. I should think about making one more
> exception for optional<T>. Though, the inliner is not short,
> not sure people will like it.

First of all, I think this would be useful addition. Quite often I do
need to supply a default value that will be returned if the conversion

However, I'd rather opt for not involving optional into this. Not only
does it make the expression longer and less clean, it introduces a
dependency on another library and makes me wonder how can I lexical_cast
optionals. I like the lexical_cast< int >("x", -1); syntax much more.

If the syntax is considered unacceptable, I can suggest another alternative:

   int x = lex_cast< int >("x").with_default(10);
   int y = lex_cast< int >("y").or_default_constructed();

The trick is that lex_cast returns a proxy that can either be converted
to int (in which case it does the regular lexical_cast) or can be used
to provide additional tweaks, like the ones above.

Obviously, the function name should be different from lexical_cast, as
it can be incompatible in some cases.

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