Boost logo

Boost :

Subject: Re: [boost] Review Request: Introduction of boost::string namespace and string-conversion functions
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2009-02-13 11:41:37


Vladimir.Batov_at_[hidden] wrote:

> 2. The name of the namespace.
>
> People largely seem content with "boost::string".

[snip]

> Therefore, unless something major comes up later we are settling on
> "boost::string".

Sorry, I still don't think that's a good idea. The problems that were
mentioned seem quite real to me, whereas the namespace name can be
easily changed to another one without problems.

If boost::strings isn't good enough, there was another suggestion - to
use the boost::algorithms namespace in the Boost.StringAlgorithms
library. Actually, submitting these tools as the StringAlgorithms
extension looks quite a viable idea to me.

> 3. Naming of the conversion functions.
>
> The "to/from/is" set seems to sit well with the majority of people.
>
> The to_string/from_string was floated around as alternatives. I feel that
> the "_string" extension indeed serves a mildly useful purpose when the
> "string" namespace is obfuscated. Like
>
> namespace boost::string obfuscated;
>
> string str = obfuscated::from(i); // What the...?
> string str = obfuscated::to_string(i); // Somewhat better but
> still...

I must say, this is my case. However, I tend to use acronyms for
namespace aliases:

   namespace bll = boost::lambda;
   namespace bmi = boost::multi_index;

As for this tool, I would probably

   namespace bsl = boost::string;

Then the library usage becomes a mess.

> When used sensibly
>
> string str = boost::string::from(i);

That's too lengthy.

> or
> namespace boost::string str;
> string str = str::from(i);

That's possible, however, str is often used as a variable name. I don't
like the idea of messing it with the namespace alias.

> the intention seems clear and the following feels like a tautology:
>
> string str = boost::string::to_string(i);

Maybe that's another sign that the namespace name is suboptimal.

> 4. String-To-Something Conversion Interface.
>
> I think people on the original
> lexical_cast-realted thread were pretty content with
>
> int i = boost::string::to<int>(str, -1);
>
> syntax. If so, then we won't need
>
> int i = to(str).or_default(-1)
> int i = to(str, default_ = -1)

I think, "to<int>(str, -1)" does not exclude "to(str, default_ = -1)".
I'd like to have both, especially since Boost.Parameter allows it. Then
I could have:

   int i = to< int >(str, -1);
   int i = to< int >(str, default_ = -1);
   int i = to< int >(str, radix_ = 16);
   bool f = to< bool >(str, bool_alpha_ = true);
   int i = to< int >(str, locale_ = loc, default_ = 100);

> 6. Something-To-String Conversion Interface.
>
> That did not seem to generate much heat at all. I'll summarize to make
> sure we see it the same way:
>
> template<class T>
> boost::string::value<std::string>
> boost::string::from(T const&) throw();
>
> Does not throw. No default value (do we need one?). Returns string() of
> failure.

I think, it should be symmetric to the "from-string" conversions and
should have the same capabilities, including default values. See my
suggestion above.

> 8. Throw/No-Throw Behavior.
>
> You might have noticed that the interface above *never* throws
anything on
> failure. I would like to highlight the fact (as it's different from
> lexical_cast behavior) and make sure people are happy with it. Those who
> need an exception thrown could do
>
> boost::value<int> i2 = boost::string::to<int>(str, -1);
> if (!i2.good()) throw something youself;
>
> Is it satisfactory?

Aside from symbol names, looks acceptable to me.


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