Boost logo

Boost :

Subject: Re: [boost] [review] Convert library
From: Vladimir Batov (Vladimir.Batov_at_[hidden])
Date: 2014-05-25 19:43:40

On 05/26/2014 04:04 AM, feverzsj wrote:
> I agree the converter concept is a step forward. And it's also a good idea to
> support both exception and defualt value on failure. The functor interface
> is useful, too.
> But as other reviewer mentioned, the convert interface seems less intuitive
> and lack a default convenient interface.
> Personally, I'd prefer something like:
> // cast like interface
> T t = conv_to<T>(from, cnv); // compare to T t =
> convert<T>::from(from, cnv).value();
> T t = conv_to<T>(from, default_v, cnv); // compare to T t =
> convert<T>::from(from, cnv).value_or(default_v);
> // plain interface
> bool re = convert(from, to, cnv);
> // functor interface
> std::transfrom(first, last, out, convert<T>(cnv)));
> std::transfrom(first, last, out, convert<T>(default_v, cnv)));
> ** with defualt interface version, "cnv" will be ignored
> Using only two names, we can get something simple and intuitive. The
> implemention can be also effectively simplfied.

1) I do like

T t = convert<T>(from, cnv); // Throws on failure
T t = convert<T>(from, default_v, cnv); // Returns 'def' on failure

and, if my memory serves me, that was pretty much review#1 interface
(minus converters). What it misses though is handling one use-case --
deterministic non-throwing handling of conversion failure. When #2
returns default_v, we do not know if it is due to a failure or "from"
just happened to convert to "default_v".

2) I can see two potential problems with a converter provided by default:

a) it might be tricky separating

convert(TypeIn const&, Converter const& =some_default());
convert(Converter const& =some_default());

b) different people might feel quite strongly what converter to be the
default... and I'll be on the receiving end of their fury. :-(

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