C++ Logo

std-discussion

Advanced search

Re: Zero-initialization in value-initialization of classes with defaulted default ctors

From: Andrey Davydov <andrey.davydov_at_[hidden]>
Date: Sun, 2 Feb 2020 13:04:31 +0300
On Sat, Feb 1, 2020 at 4:46 PM Anton Bikineev <ant.bikineev_at_[hidden]>
wrote:

> Or, even better, remove definition of `optional()` to make it
>> trivially-default-constructible?
>
> Removing the definition of optional() (or in general having it not
> user-provided) makes it deleted for T that have non-trivial default
> constructor (since T is in union).
>
> but I still don't see a reason to value-initialize `dummy`.
>
> Value-initialization appears to be needed for the constructor to be
> constexpr.
>
It seems to me that `optional` is not usable in compile-time before C++20,
because switching union active member is not allowed, and so it doesn't
make sense to mark `optional()` as constexpr, but maybe I have missed
something.
A slightly offtopic question: why in your initial example `dummy` is an
array? If it's a single object, pessimization due to zero-initialization
will be reduced (https://godbolt.org/z/PxDR8J).

-- 
Andrey Davydov

Received on 2020-02-02 04:14:24