|
Boost : |
Subject: Re: [boost] [lexical_cast] A suggestion
From: David Abrahams (dave_at_[hidden])
Date: 2009-02-09 15:56:21
on Mon Feb 09 2009, Andrey Semashev <andrey.semashev-AT-gmail.com> wrote:
> My reply may be too late for the discussion, but I'll answer anyway...
>
> David Abrahams wrote:
>
>>>> That's not my concern. My concern is that I don't think lexical_cast is
>>>> a particularly good interface for its main uses, which could be thought
>>>> of as "to-string" and "from-string."
>>> I disagree. lexical_cast was designed to be simple to use, and I think it does it
> very
>>> well. The "to-string" and "from-string" conversions, in their simple form, are just
>>> corner cases.
>>
>> Eh? What other uses has it got?!
>
> To convert objects of different types between each other.
Yes, I meant what other real-world uses has it got?
> I don't know if conversion between types that are not strings is
> widely used or not.
Then take it from me, it isn't.
> Conversions between custom and fundamental floating point types come
> to mind as an example.
Yes, hypothetically. In reality, no way. Nobody who wrote a custom
floating point type would leave its conversion to double up to the
vagaries of iostreams, especially ones that hadn't had the right
manipulators applied.
> I'm quite happy with lexical_cast, except for this default-value
> issue. Why would I write something new?
Because where you want it to go clearly doesn't mesh with the vision of
the original author or the maintainer.
>>>> I just don't think lexical_cast is the interface we need for most
>>>> convenient string conversions.
>>> Do you have a better interface in mind?
>>
>> It would take some thought and discussion to perfect, but just off the
>> top of my head, I'd want something like this:
>>
>> namespace str
>> {
>> template <class T>
>> struct value
>> {
>> operator T() const; // throws iff parse unsuccessful
>> bool parsed() const; // true iff parse succecssful
>> ...
>> };
>>
>> template <class String, class T>
>> string::value<T>
>> string::as<T>(String const&);
>>
>> template <class String, class T>
>> typename enable_if<is_integral<T>, string::value<T> >::type
>> string::as<T>(String const&, unsigned base);
>>
>> template <class T>
>> std::string
>> to_str(T const& x);
>>
>> template <class String, class T>
>> String
>> to_str(T const& x);
>> }
>
> This does look pretty much like lexical_cast, a bit extended for
> string conversions, though.
>
> I'm not against a better support for parsing or formatting in
> Boost. However, in case of simple needs, I think the potential of
> lexical_cast is not yet exhausted. Adding another library with so much
> overlapping functionality may confuse users.
Maybe, but lexical_cast clearly isn't targeted at what some large
segment of users needs.
-- Dave Abrahams BoostPro Computing http://www.boostpro.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk