Boost logo

Boost :

Subject: Re: [boost] [review] string convert
From: Matthew Chambers (matt.chambers42_at_[hidden])
Date: 2011-05-04 12:12:47


On 5/4/2011 10:32 AM, Phil Endecott wrote:
> But per my last posts, anyone can easily write those in whatever style they want, if they
> have the core function, e.g.
>
> template<typename ITER>
> void parse_int(ITER begin, ITER end, int& i, bool& success)
> {
> ....
> }
>
> int must_parse_int(std::string s)
> {
> int i; bool success;
> parse_int(s.begin(),s.end(),i,success);
> if (!success) throw ParseError();
> return i;
> }
>
> optional<int> try_parse_int(std::string s)
> {
> int i; bool success;
> parse_int(s.begin(),s.end(),i,success);
> return success ? i : optional<int>();
> }
>
> int parse_int_with_default(std::string s, int def)
> {
> int i; bool success;
> parse_int(s.begin(),s.end(),i,success);
> return success ? i : def;
> }

It's not clear what complexity you're eliding and what you are really excluding. I don't think
completely non-generic overloads like you've suggested will fly in boost. It also seems to emphasize
string->type. What about int->string?

Although you do bring up the interesting case of converting a string iterator_range. I think that is
a very reasonable use case (to avoid copying). But I think we can do it with a single iterator_range
argument instead of a pair of iterators. For example, it would be nice to be able to do:
string node = "<value>1234</value>";
lexical_cast<int>(iterator_range(node.begin()+7, node.begin()+11));

-Matt


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