C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Re-Init Example Use Case

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 14 Aug 2022 22:57:34 -0400
On Sun, Aug 14, 2022 at 10:06 PM Greg McPherran via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Text editor implemented as a vector of strings. Each string in vector is a line of text.
>
> vector implementation internally leverages re-init by cache'ing the string objects for removed lines of text. When new lines of text are created e.g. by emplacement, the vector can determine whether to construct a new string or re-init an existing string object based on availability of what is in the cache.

That's not how `vector` works. There is no "cache", nor is there any
"re-init an existing string object".

`vector<T>` holds an array of `T`s, but that storage has a maximum
capacity that is separate from its size. If elements need to be
inserted, and the number to be inserted would be below the capacity,
then no new memory is allocated. New `T`s are constructed, but no
memory needs to be allocated.

> Currently, this would be handled by std::construct_at(...) using an instance rvalue. This is fine for raw empty memory, but for existing instances in the vector purposefully made available for re-use,

There are no such things in `vector`, nor do there need to be. You
don't seem to understand that memory *allocation* is the expensive
part, not object construction. `vector`s can have additional space to
avoid allocation, not to avoid construction.

> the "emplace using re-init feature syntax" is simpler and more concise, and incorporates recognition of this pattern into the language.

What "emplacement using re-init feature syntax"? You have proposed no
such syntax, so claiming that it is "simpler and more concise" is just
wrong. And since the pattern you claim needs recognition *does not
exist* in the very type you're talking about... this whole
conversation seems really cart-before-the-horse.

> What is std::xyz today may be a prototype for tomorrow's language syntax and I propose that the re-init ctor is an example.
>
> I also maintain that unique_ptr::reset() is a perfectly valid example of usefulness of re-init concept.

You can maintain whatever you like, but that doesn't change the fact
that it's *just one type*. You're talking about adding something to
the *language*. Nobody is stopping you from adding `reset` or whatever
you want to call it to any object you feel needs such a concept. But
if you want to change the language, you can't just point to a single
function in a single type and claim that this is a concept that needs
to be in the language.

Received on 2022-08-15 02:58:00