Boost logo

Boost :

Subject: Re: [boost] Convert library -- Andrzej's review
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2014-05-20 07:58:08


2014-05-20 6:34 GMT+02:00 Vladimir Batov <Vladimir.Batov_at_[hidden]>:

>
> On 05/18/2014 11:45 PM, Vladimir Batov wrote:
>
>> Vladimir Batov <vb.mail.247 <at> gmail.com> writes:
>>
>> Andrzej Krzemienski <akrzemi1 <at> gmail.com> writes:
>>>
>>> I personally do not like this trick to force me to type this "from" in
>>>> "convert<int>::from".
>>>>
>>> I just had another look and there seems no value anymore in potential
>>>
>>> specializations on TypeIn, TypeOut. It's because all the "smartness" and
>>> type-handling has been moved to converters. So, *there seems*, the "from"
>>> can be dropped. That is,
>>>
>>> int v = boost::convert<int>(str, cnv).value();
>>>
>>> instead
>>>
>>> int v = boost::convert<int>::from(str, cnv).value();
>>>
>>> and
>>>
>>> std::transform(
>>> strings.begin(),
>>> strings.end(),
>>> std::back_inserter(integers),
>>> convert<int, string>(ccnv(std::hex)).value_or(-1));
>>>
>>> instead
>>>
>>> std::transform(
>>> strings.begin(),
>>> strings.end(),
>>> std::back_inserter(integers),
>>> convert<int>::from<string>(ccnv(std::hex)).value_or(-1));
>>>
>>> I am on the fence (the "from" has been with me for some time). Any
>>> strong/weak :-) preferences, any for/against arguments?
>>>
>>>
> Given a few people were not happy with boost::convert::from() interface
> I've added boost::cnv() for now (so that the names do not clash while both
> exist).
>
> int v = boost::cnv<int>(str, cnv).value();
>
> does look cleaner and somewhat similar to lexical_cast deployment (for
> better or worse). However, I feel similarly confused about
>
> boost::cnv<int, string>(cnv).value_or(-1);
>
> as I always felt about lexical_cast when I had to specify both types
>
> boost::lexical_cast<int, string>("char string");
>
> compared to more explicit
>
> convert<int>::from<string>(cnv).value_or(-1);
>
> I am easy about both ways and will go with whatever the majority settles
> on. Any feelings/preferences/arguments for one or the other?

A bike-shed issue, isn't it? Either choice has its flaws. I guess the best
solution is that you make your chocie, and use it, unless someone is able
to convince you otherwise. One question though, regarding your last example:

  convert<int>::from<string>(cnv).value_or(-1);

this renders a "converter object" with the following signature:

  int operator()(std::string const&); // right?

It is an equivalent of a "monomorphic" lambda. I guess you could invent
another syntax, that does not require to specify this InType:

  convert<int>(cnv).value_or(-1);

which would render a converter object with parametrized signature:

  template <typename InType>
  int operator()(const InType&);

This would be an equivalent of a polymorphic lambda. This appears more
useful, at the first sight. Any reason why you choose not to do so?

Regards,
&rzej


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