Boost logo

Boost :

Subject: Re: [boost] Variadic append for std::string
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2016-12-31 18:21:25


On 12/31/16 20:36, Olaf van der Spek wrote:
> On Thu, Dec 29, 2016 at 2:53 PM, Andrey Semashev
> <andrey.semashev_at_[hidden]> wrote:
>>
>> I already mentioned in the std-proposals discussion that I don't think
>> formatting should be dealed with by std::string or a function named
>> append().
>
> It'd be helpful if you include *why* you think so..

I've already explained my opinion in the std-proposals discussion. For
the sake of completeness, here's a short version:

It's simply not std::string's job to do the formatting, IMO. This class
should be nothing more than a container of characters (well, it is
slightly more now, but I don't consider that a good thing). I guess,
that's mostly because I believe one class should be responsible for
doing only one thing, and in case of std::string it's storing a string.
Adding formatting functionality to std::string would increase the class'
bloat in terms of interface and implementation and likely add new
dependencies. Though, this is probably not an argument against a
separate non-intrusive library.

Back to the proposal for Boost, I don't mind if there is a standalone
function or library that does the formatting, as long as it offers some
advantage over the existing libraries. The proposed function though
should not be named `append` IMO because it's not the primary thing the
function does. I would expect an `append` algorithm to be generic and
compatible with any container, i.e. something that does nothing more
than `c.insert(c.end(), x)` or `c.append(x)`, for `x` being every
argument in the list of arguments to be appended. I.e. this should work:

   std::list< double > c{ 1.0, 2.0, 3.0 };
   append(c, 10.0, 20.0, 30.0); // calls c.insert()

As well as this:

   std::string s{ "Hello" };
   append(s, ", world!", " Happy 2017! :)"); // calls s.append()

This, however:

   append(s, 47);

should result not in appending "47" but in appending "/" (a character
with code 47). I can see how this could be confusing to someone, but
that is what you'd get from calling `s.insert()` manually, and what I'd
expect from a function called `append`.

If formatting is required I would prefer to be required to spell my
intent more clearly, like this:

   print(s, 47);

or:

   format(s) << 47;

Also, I'm not clear enough about the intended use cases of the proposed
library. Is the goal just to optimize memory allocation? Is that at all
possible when formatting is involved? Would it be better than snprintf
into a local buffer?

Does the library open new use cases? For example, someone suggested in
the std-proposals discussion something similar to this:

   throw std::runtime_error(format(std::string()) << "Error " << 47);

(I wrapped the default-constructed std::string() into format(), because
I don't think overloading operator<< for std::string is an acceptable
approach for the same reasons I mentioned above.)

I think, something with one line capability like that would be useful.
Would the library allow something like this?

Would the library support targets other than std::string? E.g. would I
be able to format into an `std::array< char, 10 >`?


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