C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A type trait to detect if value initialization can be achieved by zero-filling

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 29 Jan 2023 16:11:48 -0500
On Sun, Jan 29, 2023 at 3:41 PM Giuseppe D'Angelo via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hello,
>
> As per subject, I've been working on a proposal for a standardized way
> to detect if a trivially default constructible type can be value
> initialized by using `memset(0)` on some suitable storage.
>
> A draft is available here:
>
> https://isocpp.org/files/papers/D2782R0.html
>
> As usual, any early feedback is very much appreciated. I am not 100%
> convinced about the name of the trait, as I'm trying to model something
> for which there isn't already an established name/concept. I hope the
> discussion/bikeshedding can wait after everything else has been ironed
> out...

I don't like that the set of such types is so broadly
implementation-defined. We need something more concrete.

I would suggest that a type is "trivially zero-initializable" if:

1. If it is a class type:
   1a. It is an implicit lifetime type
   1b. All of its non-static data members and base classes are
themselves implicit lifetime types
2. It is an arithmetic type
3. It is an enumeration type
4. It is `nullptr_t`
5. The other fundamental types are all implementation defined.
However, I would suggest that it should be equally true/false for all
pointers of the same category: object pointers, function pointers, and
member pointers. Either all object pointers are
trivially-zero-initializable or none of them are. Etc.
6. No reference types are trivially-zero-initializable.

Also, how does Itanium-based code work if you have a member pointer in
static storage that gets zero-initialized? Does something have to
manually fill in the -1s at runtime, or does an object containing such
a member pointer have to take up space in static executable storage?

Received on 2023-01-29 21:12:19