C++ Logo

std-proposals

Advanced search

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

From: Julien Villemure-Fréchette <julien.villemure_at_[hidden]>
Date: Sun, 14 Aug 2022 23:20:19 -0400
This seems to me this discussion exhibits the A/B problem since the begging. For the mailing list, it doesn't sound very clear how how it would be beneficial to add a built-in feature when something which is already builtin the language exits for in place construction (placement new, does not allocate btw). In general. adding a second way to do something is not good unless it help reduce boilerplate and has enough use in *canonical* C++ (code written along the lines of std lib, boost, cppCoreguidelines).

Unless you are working on the design of a conformimg AllocatoAwareContainer, or boxing-like types (std::optional, std::variant, etc) you should never need to touch std::construct_at, std::destroy_at, placement new, explicit dtor calls.

As for your example (txt editor): calling `vector::reserve` and using move semantics. swaps, and in place insertion; to me that seems to solve issues of superficial reallocation.

Note: things like two step init is an antiguideline (at least in c++ way of expressing ideas). The canonical *C++*, has a different approach for this.


-------- Original Message --------
From: Greg McPherran via Std-Proposals <std-proposals_at_[hidden]>
Sent: August 14, 2022 10:06:36 p.m. EDT
To: "std-proposals_at_[hidden]" <std-proposals_at_[hidden]>
Cc: Greg McPherran <GMcPherran_at_[hidden]>
Subject: Re: [std-proposals] Re-Init Example Use Case

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.

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.

Anyway, I've stated my case. And with regards to "tossing" ideas, as I mentioned, if a language designer would like me to further collaborate, I am willing and I already offer the some_object_instance::(ctor_args) syntax suggestion.

Have Great Night All,
Greg McPherran

-- Julien Villemure
Sent from my Android device with K-9 Mail.

Received on 2022-08-15 03:20:30