Boost logo

Boost :

Subject: Re: [boost] Review Request: Introduction of boost::string namespace Re: Review Request: Introduction of boost::string namespace
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2009-02-12 09:12:32


Stewart, Robert wrote:
> From: Phil Endecott
>>
>> What do people think about the string/number conversions in N2408 ?
>> (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2408.html -
>> what is the status of that?) Unless there's something wrong
>> with that, I think we should build on it rather than re-inventing
>> the wheel.
>
> Those are C-style. The advantage of a template is that the compiler deduces
> the correct implementation.

The compiler can also deduce the correct implementation using
overloading, which is what N2408 proposes for its to_string functions:

     string to_string(long long val);
     string to_string(unsigned long long val);
     string to_string(long double val);

For the from_string functionality we hit the standard problem that we
can't type match on the type of the thing that the result is being
assigned to:

     short a = from_string(s);
     float f = from_string(s);

The compiler can't deduce the correct implementation; we have to tell
it (unless we pass the result by reference I suppose). We have a
choice of template syntax:

     short a = from_string<short>(s);
     float f = from_string<float>(s);

or the "C like" syntax proposed by N2408 (which I've just noticed
doesn't mention short anywhere, breaking my example):

     int i = stoi(s);
     float f = stof(s);

In terms of syntax the latter is clearly more concise; some might argue
that it's too concise (i.e. cryptic), but I would say that these
conversions and their C equivalents are sufficiently commonly used that
users will quickly learn what they do. But apart from these syntax
differences, are there any benefits to one version rather than the other?

Another feature of N2408 is that it supports number bases other than 10
- for from_string conversions at any rate - using a defaulted
parameter. I would value this along with precision for floating-point types.

Phil.


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