Title
Zero- and value-initialization of union objects
Status
c++11
Section
9.4 [dcl.init]
Submitter
Clark Nelson

Created on 2008-05-14.00:00:00 last changed 123 months ago

Messages

Date: 2010-11-15.00:00:00

[Voted into the WP at the November, 2010 meeting.]

Date: 2010-10-15.00:00:00

Proposed resolution (October, 2010):

Change 9.4 [dcl.init] paragraph 5 as follows:

To zero-initialize an object or reference of type T means:

  • if T is a scalar type (6.8 [basic.types]), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;103

  • if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized, and padding is initialized to zero bits;

  • if T is a (possibly cv-qualified) union type, the object's first non-static named data member is zero-initialized, and padding is initialized to zero bits;

  • if T is an array type, each element is zero-initialized;

  • if T is a reference type, no initialization is performed.

Date: 2010-02-15.00:00:00

Additional notes (February, 2010):

The C Committee has changed its approach to this question and is no longer using a two-phase process; in C, zero-initialization is specific to program start-up and thus is not appropriate for use with thread-local storage. See C paper N1387.

Date: 2008-09-15.00:00:00

Notes from the September, 2008 meeting:

It was observed that padding bytes in structs are zero-initialized in C, so if we are changing the treatment of unions in this way we should consider adding the C behavior for padding bytes at the same time. In particular, using memcmp to compare structs only works reliably if the padding bytes are zero-initialized.

Date: 2008-08-15.00:00:00

Proposed resolution (August, 2008):

Change bullet 3 of 9.4 [dcl.init] paragraph 5 (in the first list, dealing with zero-initialization) as follows:

  • if T is a (possibly cv-qualified) union type, the object representation of T (6.8 [basic.types]) is zero-initialized, after which the object's first non-static named data member is zero-initialized;

[Drafting notes: Ask a C liaison about the progress of WG14 paper N1311, which deals with this issue. Since the adoption of WG21 paper N2544, unions may have static data members, hence the change to refer to the first non-static data member and the deletion of the footnote.]

Date: 2008-05-14.00:00:00

The C committee is considering changing the definition of zero-initialization of unions to guarantee that the bytes of the entire union are set to zero before assigning 0, converted to the appropriate type, to the first member. The argument (summarized here) is for backward compatibility. The C++ Committee may want to consider the same change.

History
Date User Action Args
2014-03-03 00:00:00adminsetstatus: fdis -> c++11
2011-04-10 00:00:00adminsetstatus: dr -> fdis
2010-11-29 00:00:00adminsetmessages: + msg3176
2010-11-29 00:00:00adminsetstatus: tentatively ready -> dr
2010-10-18 00:00:00adminsetmessages: + msg2984
2010-10-18 00:00:00adminsetstatus: drafting -> tentatively ready
2010-02-16 00:00:00adminsetmessages: + msg2572
2009-11-08 00:00:00adminsetstatus: review -> drafting
2008-10-05 00:00:00adminsetmessages: + msg1766
2008-08-25 00:00:00adminsetmessages: + msg1751
2008-08-25 00:00:00adminsetstatus: open -> review
2008-05-14 00:00:00admincreate