|
Boost : |
Subject: Re: [boost] Review Request: Introduction of boost::string namespace and string-conversion functions
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-02-11 15:05:39
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of
Vladimir.Batov_at_[hidden]
> I would like to propose to create a new
>
> boost::string
>
> namespace where I'd expect all string-related functionality might be
> logically housed.
Sounds right.
> >From the user point the interface I'd like to propose might
> look like the
> following:
>
> int i;
> std::string s;
>
> 1) int i_from_s = boost::string::to<int>(s); // throws
> 2) std::string s_from_i = boost::string::from<int>(i);
> 3) bool s_is_i = boost::string::is<int>(i);
"is" is a poor name choice. It doesn't imply "can convert to." What's more, in order to arrive at the answer, I imagine one must actually try the conversion, so it would be better to provide a conversion function that returns false rather than throwing an exception on failure:
if (boost::string::from<int>(s, i))
and
if (boost::string::to<int>(i, s))
(I always put mutable parameters first in my code, but I can see an argument that they should follow the order of the types in the qualified call: string::to<int>(s, i).)
> 4) int i_from_s = boost::string::to<int>(s)(-1);
>
> The above is a non-throwing variation of #1 with the default value
> provided for failed conversion.
That would read better as:
int j(boost::string::to<int>(s).or(-1));
> 5) int i_from_s = boost::string::to<int>(s)(std::hex);
> 6) std::string s_from_i = boost::string::from<int>(i)(std::hex);
>
> The above is variations of #1 and #2 with the std::hex
> manipulator/formatter applied.
This is interesting, but I don't like the new syntax relative to IOStreams. How about this:
int l(boost::string::to<int>(s) >> std::hex);
This implies a stream in the object returned from to<int>(s) on which the manipulator can be applied.
> 7) int i_from_s =
> boost::string::to<int>(s)(-1)(std::hex)(yet-another-manipulator);
That looks odd.
> Off the cuff the actual conversion might look something along the
> following lines:
>
> template<class T, class Char>
> boost::string::value::operator T()
> {
> std::basic_istringstream<Char> stream(string_);
>
> if (manipulators) stream >> manipulator_ >> ... >> value_;
> else stream >> value_;
if (manipulators) stream >> manipulator1 >> ...;
stream >> value_;
> if (stream.fail())
> if (no_default) throw something;
> else return default_value_;
> return value_;
That could be managed in derived value types based upon the signature of the calling function rather than using data members and conditional logic to react.
_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com
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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk