Subject: Re: [boost] [review] string convert
From: Gordon Woodhull (gordon_at_[hidden])
Date: 2011-05-04 10:47:51
On May 4, 2011, at 10:33 AM, Stewart, Robert wrote:
> The only other one I haven't commented on is the pair<bool,int> version. It shouldn't require a second argument. All of the variations that take a single argument should have a version that takes a second in case there's no default construction or zero-initialization possible. That aside, the pair version leads to much less readable code, not least because the semantics of pair<int,bool> are less obvious than those of optional<int>.
We're going in circles because it's hard to keep all the use-cases in one's head at one time.
Something is needed here, when you want to use the fallback but you also want to know if conversion succeeded. See that snippet from Vladimir that I just quoted again in my reply to Matt.
> Compare this:
> auto r(convert_cast<optional<int>>(s));
> if (r)
> i = r.get();
> versus this:
> auto r(convert_cast<pair<int,bool>>(s));
> if (r.second)
> i = r.first;
> Even better:
> auto r(try_convert_cast<int>(s));
> if (r)
> i = r.get()
> Notice also that the precedent, std::map::insert()'s return type, puts bool second, not first. I never remember the order and you used the reverse. That leaves too much room for confusion.
> The optional version is much better.
But not sufficient.
I think I'm done. I hope Ed or someone feels like making some sense out of all this. :-D
Boost list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk