C++ Logo

liaison

Advanced search

[wg14/wg21 liaison] N2900 Initialization with {} and potential semantic divergence from C++ for new-to-C syntax

From: Hubert Tong <hubert.reinterpretcast_at_[hidden]>
Date: Thu, 3 Mar 2022 01:17:50 -0500
The wording in N2900 uses the static initialization wording for
initialization with {}. For the case of a union, the static initialization
is described as:

> the first named member is initialized (recursively) according to these
> rules, and any padding is initialized to zero bits
>

Firstly, as not a purely interoperability concern, the definition of
padding here is unclear. Does this refer to padding added to the end of the
union to achieve the required alignment in cases where the largest member
does not have the greatest alignment requirement? Does it refer to the
union of the trailing padding in the union type and the intersection of all
the alignment padding and the padding bits within integer subobjects of the
union members?

It seems the presence of a separate paragraph in 6.2.6, "Representation of
types" to talk about bytes that correspond to other members of a union
after talking in the preceding paragraph about padding bytes mean that the
bytes corresponding to other members are *not* automatically considered
padding. This interpretation is the one that is consistent with the idea
that what constitutes padding is a static property of a type and not a
dynamic property of an object.

Secondly, the semantics specified does not match what C++ does for the same
syntax.

>From [dcl.init.aggr]:
If the aggregate is a union and the initializer list is empty, then
— if any variant member has a default member initializer, that member is
initialized from its default
member initializer;
— otherwise, the first member of the union (if any) is copy-initialized
from an empty initializer list.

Only the first member is initialized. The "padding" is left uninitialized.

Received on 2022-03-03 06:18:19