C++ Logo

std-discussion

Advanced search

Re: On "transparently replaceable" in std::vector operations

From: Hyman Rosen <hyrosen_at_[hidden]>
Date: Tue, 27 Apr 2021 12:36:29 -0400
The C++ object model is fundamentally broken. The correct definition for an
object is a region of storage plus a type to interpret it's contents. The
wrong definition used by C++ requires that objects be "created", and have
"lifetimes" that "begin" and "end" when certain functions are called, and
that pointers and references refer to created objects, not to regions of
storage. This leads to the nonsense that you have observed.

On Tue, Apr 27, 2021, 11:00 AM Giuseppe D'Angelo via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> Hi,
>
> 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`
> onwards.
>
> 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
>
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2021-04-27 11:36:45