Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] A suggestion
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2009-02-07 05:25:17


Scott McMurray wrote:

> One very clear use:
>
> if (optional<int> oi = lexical_cast<optional<int> >(mystring)) {
> int i = *oi;
> ... use i ...
> } else {
> ... parse failed ...
> }
>
> And if you want defaults:
>
> int i = -1;
> if (optional<int> oi = lexical_cast<optional<int> >(mystring)) {
> i = *oi;
> }
> ... use i ...
>
> Or, using the helper:
>
> int i = lexical_cast<optional<int> >(mystring).get_value_or(-1);
>
> Why is this so bad?

Like I said:

1. It is longer and more clumsy than lexical_cast< int >(str, 1). It
does introduce enough scaffolding that user has to type to avoid
lexical_cast altogether in such cases.
2. It introduces dependency on optional.
3. It bans lexical_casting optionals.


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