C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Appending a string to itself

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 21 Jul 2023 17:39:52 -0700
On Friday, 21 July 2023 13:02:24 PDT Jonathan Wakely via Std-Proposals wrote:
> You *could* allocate, copy the old string into the new storage, deallocate
> old storage, then copy the argument into the new storage (which can't throw
> because you're just copying trivial types). That wouldn't work, and is no
> easier than just doing it so it works properly, so there's no reason to do
> it.

The more likely scenario for performance is using realloc(), which may move
the data.

std::string isn't allowed that, though. We do that for QString and QByteArray,
which means we must be careful about inserting a portion of the string into
itself.

The right solution would be to have a realloc_no_move().

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-07-22 00:39:55