C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: Zero-initialization in value-initialization of classes with defaulted default ctors
From: Andrey Davydov (andrey.davydov_at_[hidden])
Date: 2020-02-01 01:04:30


On Fri, Jan 31, 2020 at 6:07 PM Anton Bikineev via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> The Standard says:
>
> To *value-initialize*
>> <http://eel.is/c++draft/dcl.init#def:value-initialization> an object of
>> type T means:
>>
>> - - if T is a (possibly cv-qualified) class type with either no
>> default constructor or a default constructor that is user-provided or
>> deleted, then the object is default-initialized;
>>
>>
>> - - if T is a (possibly cv-qualified) class type without a
>> user-provided or deleted default constructor, **then the object is
>> zero-initialized** and the semantic constraints for
>> default-initialization are checked, and if T has a non-trivial
>> default constructor, the object is default-initialized;
>>
>>
>> - - ...
>>
>>
> I'm wondering, why in C++11 an object needs to be zero-initialized (this
> was not the case before C++11)? I can't find a proposal/DR that changes
> that, but I suspect that this is to align with value-initialization of
> scalar types (which implies zero-initializaiton).
>

I'm sure symmetry with value-initialization of scalar types is the reason.
It would be very strange if for the `pair<int, array<int, 1>> p;` p.first
== 0, but p.second[0] has indeterminate value.

> The problem is that this leads to suboptimal code generation:
> https://godbolt.org/z/pwYjLP (this is a minimized example from optional
> implementation in abseil).
>

Is it possible just to remove mem-initializer for `dummy` (
https://godbolt.org/z/22zBTu)? Or, even better, remove definition of
`optional()` to make it trivially-default-constructible?

-- 
Andrey Davydov


STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups