C++ Logo


Advanced search

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

From: Anton Bikineev <ant.bikineev_at_[hidden]>
Date: Fri, 31 Jan 2020 16:07:17 +0100
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).

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).

In case of empty structs, I don't think that zero-initialization (and
value-initialization conceptually) is meaningful, since empty structs don't
have/store any value.


Received on 2020-01-31 09:10:04