C++ Logo

std-discussion

Advanced search

Re: Vector implementation of behavior when inserting objects whose copy constructor can throw

From: Bo Persson <bo_at_[hidden]>
Date: Tue, 27 Jul 2021 23:00:17 +0200
On 2021-07-27 at 22:42, Andrey Semashev via Std-Discussion wrote:
> On 7/27/21 10:48 PM, Giuseppe D'Angelo via Std-Discussion wrote:
>> Hello,
>>
>> On 27/07/2021 21:09, Andrey Semashev via Std-Discussion wrote:
>>> On 7/27/21 6:29 PM, Jason McKesson via Std-Discussion wrote:
>>>> On Sun, Jul 25, 2021 at 3:21 PM Andrey Semashev via Std-Discussion
>>>> <std-discussion_at_[hidden]> wrote:
>>>>>
>>>>> There is always the solution to always reallocate the storage and
>>>>> do the
>>>>> full copy with the inserted element.
>>>>
>>>> That isn't allowed. `insert` is required to maintain the validity of
>>>> pointers/references to elements before the insertion point *unless*
>>>> reallocation occurs.
>>>
>>> Exactly. The important part is "unless reallocation occurs".
>>>
>>>> And reallocation is only *permitted* to occur if
>>>> the number of new elements inserted exceeds the available capacity -
>>>> size.
>>>
>>> Is it? I don't see where the spec has such restriction for insert. There
>>> is one for e.g. reserve, but not for insert.
>>

The description for reserve also mentions insertion:

"No reallocation shall take place during insertions that happen after a
call to reserve() until an insertion would make the size of the vector
greater than the value of capacity()."

I don't think the intention here is that the vector should keep track of
whether reserve() has been called and use different insert methods in
that case.

Received on 2021-07-27 16:01:09