Boost logo

Boost :

Subject: Re: [boost] Variadic append for std::string
From: Olaf van der Spek (ml_at_[hidden])
Date: 2017-01-03 06:02:47


On Thu, Dec 29, 2016 at 1:19 AM, Yakov Galka <ybungalobill_at_[hidden]> wrote:
> On Tue, Dec 27, 2016 at 4:47 PM, Olaf van der Spek <ml_at_[hidden]> wrote:
>
>> One frequently needs to append stuff to strings, but the standard way
>> (s += "A" + "B" + to_string(42)) isn't optimal due to temporaries.
>>
>
> Can't we already write it through (((s += "A") += "B") += to_string(42))?
> This is the time I think that assignment operators, other than =, should
> have had left associativitiy... pity they don't.

<< does the trick:

s << "A" << "B" << 42;

std::string& operator<<(std::string& os, std::string_view v)
{
    return os += v;
}

std::string& operator<<(std::string& os, long long v)
{
    return os += std::to_string(v);
}

-- 
Olaf

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