Boost logo

Boost :

Subject: Re: [boost] [conversion] try_lexical_cast and 200$
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2013-12-12 09:05:20


2013/12/12 Steven Watanabe <watanabesj_at_[hidden]>
<...>

> Personally, I'm a bit unhappy
> about this trend of creating parallel API's
> that throw or report an error in some other way.
>

I had exactly the same opinion two years ago. Original developer of
lexical_cast had the same opinion.

But now I have code like this one:

// Function that maps to some query language method
inline boost::variant<double, error_t> to_double(std::string const& val) {
    double ret;
    std::istringstream ss(val);
    ss >> ret;
    if (ss.fail() || !ss.eof())
        return error_t("'to_double' function called for value '" + val +
"'");
    return ret;
}

A lot of people, including me, would prefer to have the following:

inline boost::variant<double_t, error_t> to_double(std::string const& val) {
    double ret;
    if (!try_lexical_cast(val, ret))
        return error_t("'to_double' function called for value '" + val +
"'");
    return ret;
}

Such method would also allow to use lexical_cast library for non default
constructible types.
While I sill dislike the idea of having additional overload. That's why
I've asked for comments.

-- 
Best regards,
Antony Polukhin

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