Boost logo

Boost :

Subject: Re: [boost] [review] string convert
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-05 13:35:35

Matt Chambers wrote:
> On 5/5/2011 6:48 AM, Stewart, Robert wrote:
> > optional<int> o(try_convert_cast<int>(str));
> > int i;
> > if (!o)
> > {
> > std::cout<< "using fallback\n";
> > i = fallback;
> > }
> > else
> > {
> > i = o.get();
> > }
> >
> > That's clearly inconvenient and 5 doesn't apply because the
> > optional won't be set if the conversion fails. To address
> > such inconvenience, you offered convert<T>::result and
> > pair<optional<T>,bool> has been suggested. Isn't optional
> > redundant in the latter?
> What happened to:
> int i = fallback;
> if (!try_convert_to<int>(str, i)) { cout << "using fallback"; }

It slipped my mind in the whirl of ideas being bandied about in this thread.

> A good postcondition is that try_convert_to does not modify i
> if the conversion fails. This achieves the same thing and is
> honestly more convenient and transparent than the
> convert<T>::result approach.


> I suppose you could argue that the preliminary assignment is
> inefficient, but I can't see a reasonable objection to it.

For a type like int, it really doesn't matter. For UDTs, there might be some slight penalty, but I think it would be an edge case.

OK, that means we're down to the following now:

 - default_value<T> customization point
 - converter<T,S> customization point, main logic, functor
 - T convert_cast<T,S>(S, formatting = none); can throw
 - T convert_cast<T,S>(S, T, formatting = none)
 - bool try_convert_to<T,S>(S, T &, formatting = none)

I removed the following from my earlier list because I think try_convert_to() is a suitable replacement:

 - optional<T> name_me_1<T,S>(S, formatting = none)
 - optional<T> name_me_1<T,S>(S, T, formatting = none)
 - pair<T,int> name_me_2<T,S>(S, T, formatting = none)

(The second is not needed because of the default_value CP.)

Clearly, try_convert_to() can be the foundation of the other function templates. Phil's suggestion was to forego the others and just provide the foundation. I'm not sure that's necessary, but it remains an option.

It's worth enumerating the use cases:

1. lexical_cast-style usage: new-style cast with exception
2. new-style cast with fallback and no exceptions
3. indicate whether the conversion succeeded; no value if failed
4. indicate whether the conversion succeeded; use fallback

Is that a complete list? I suspect I'm missing something.

1 is satisfied by T convert_cast<T,S>(S, formatting = none).

2 is satisfied by T convert_cast<T,S>(S, T, formatting = none).

3 is satisfied by try_convert_to(), though by virtue of having first created a T to pass as the second argument. name_me_1 would be a more direct way to satisfy this use case.

4 is satisfied by try_convert_to().

Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

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