C++ Logo

std-proposals

Advanced search

Re: C++ create a class with array members which arenot constructed.

From: Jefferson Carpenter <jeffersoncarpenter2_at_[hidden]>
Date: Thu, 3 Sep 2020 07:15:18 +0000
On 9/2/2020 9:52 PM, Steve Hearnden via Std-Proposals wrote:

> Reviewing existing containers left me considering the code is less clear and more complicated than it needs to be due to the requirement to ensure that the compiler doesn’t see the reserved space as the type (or an array of the type), to ensure the compiler doesn’t construct / destruct the item(s).

That's certainly the crux of the matter. If the compiler doesn't
construct the items, then they may or may not be constructed at runtime
when the member is copied, moved, or destructed. This is why union
itself has certain limitations on the kinds of constructors its members
can have.

For [[uninitialized]] not to place the same restrictions as union does
on the constructors and assignment operators of the class using it, the
runtime would need to track the initialization status of the array elements.

std::optional and std::variant both do this with auxiliary data members.


https://tools.ietf.org/html/rfc1925 (6a) "It is always possible to add
another level of indirection."

-Jefferson

Received on 2020-09-03 02:18:56