C++ Logo


Advanced search

On "transparently replaceable" in std::vector operations

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Tue, 27 Apr 2021 17:00:57 +0200

Suppose I have a `std::vector<X>` with sufficient extra capacity to
insert new elements without reallocation.

[vector.modifiers] says that inserting an element at position `p`
invalidates all iterators, references and pointers from position `p`

I'm wondering why there's such a requirement for pointers (and
references), considering the paragraph about "transparently replaceable"
objects in [basic.life]:

> http://eel.is/c++draft/basic.life#8

Couldn't that paragraph be used to guarantee that, in fact, pointers and
references after a non-reallocating insertion are still valid, but they
just refer to "other" objects in the vector -- depending on the their
position? For instance, in:

> X* ptr = v.data();
> v.insert(v.begin(), x);
> use(*ptr); // <--

could the last line be perfectly valid (not even a launder() required),
pointing to the newly inserted element?

Of course the discussion can be generalized to erasure.

Thanks for the insights,
Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

Received on 2021-04-27 10:01:23