C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal for skipping initialization in vector and complex constructors

From: Bo Persson <bo_at_[hidden]>
Date: Sun, 10 Nov 2024 12:29:54 +0100
On 2024-11-10 at 11:04, André Offringa via Std-Proposals wrote:
> Dear all,
>
> I'd like to float the idea to make it possible to construct a
> std::complex and a std::vector uninitialized. Basically, I would propose
> something like the following to exist.
>
> A tag structure:
>
> struct skip_initialization_t {};
> constexpr skip_initialization_t skip_initialization;
>
> And the constructor overloads for std::vector and std::complex that have
> this tag as parameter, that do not initialize their data values.
>
> For vector, the overload:
>
> // Construct a vector with n elements without initializing them
> vector(size_t n, skip_initialization_t, const allocator_type& allocator
> = Alloc())
>
> ...and optionally "for consistency" but less often used:
>
> // Set the size of the vector to n elements; new elements are left
> uninitialized
> void resize(size_t n, skip_initialization_t)
>
> // Add n uninitialized values at the given position
> // (maybe too obscure...)
> iterator insert(const_iterator position, size_t n, skip_initialization_t)
>

Yes, very obscure. This sounds like a footgun fully loaded.

At least it has to be restricted on what types it can be used for, which
makes it even more of a niche feature.

Just consider if someone tries this on a vector<string> and then,
somewhere else in the program, there is an exception thrown. What
happens to a vector of undestructible objects? Or if, by a slight
mistake, the vector is resized again before all the objects have been
constructed?

Received on 2024-11-10 11:29:57