Boost logo

Boost :

Subject: Re: [boost] Variadic append for std::string
From: Christof Donat (cd_at_[hidden])
Date: 2017-01-03 09:16:27


Hin

Am 03.01.2017 14:20, schrieb Olaf van der Spek:
> On Tue, Jan 3, 2017 at 2:19 PM, Christof Donat <cd_at_[hidden]> wrote:
>> Am 01.01.2017 00:21, schrieb Andrey Semashev:
>>>
>>> throw std::runtime_error(format(std::string()) << "Error " << 47);
>>
>>
>> How would that differ from
>>
>> throw std::runtime_error((std::ostringstream{} << "Error " <<
>> 47).str());
>
> Simpler syntax, better performance

I see the chances for better performance, but for the syntax I don't
really see
any remarkable improvements.

If performance matters, I'd try with boost::spirit::karma. The syntax
will be
less concise, but I am not aware of a faster generic solution.

   auto message = std::string{10}; // <- preallocate enough memory for
the message
   if( !karma::generate(std::begin(message), ascii::space, "Error " <<
karma::uint_, 47) ) {
       // formating the error message failed. throw something else.
   }
   // since this is the exit of the function, the compiler might apply
copy elision.
   throw std::runtime_error(message);

If you have multiple places like that in your code, I guess, you'd like
to wrap it
into a generic function and you have a similar API to the "append()"
proposal. Now
I see, how it might be useful, thanks. I think, append() should rely on
karma
generators then, instead of yet another int to string implementation,
because we
only already have five dozens.

Christof


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