C++ Logo

std-proposals

Advanced search

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

From: Bo Persson <bo_at_[hidden]>
Date: Mon, 15 Aug 2022 10:26:41 +0200
On 2022-08-15 at 04:06, Greg McPherran via Std-Proposals 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.

For this example. ypu might want to check how much work is done when
constructing a new string from scratch:

    std::string test;
00007FF6E6DAFE84 mov word ptr [test],0

It sets size to zero, and that's it. How do you improve on that by reusing?



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

And std::string also has a clear() member that sets the size to 0, but
keeps any allocated resources for later reuse. So does std::vector.

So you would need better examples of where the feature would be widely
useful.

Received on 2022-08-15 08:26:47