CWG Issue 1460 (original) (raw)

This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 117a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-04-13


1460. What is an empty union?

Section: 11.5 [class.union]Status: C++14Submitter: Jason MerrillDate: 2012-02-08

[Moved to DR at the September, 2013 meeting.]

When a similar question was raised in issue 413, the resolution was to remove the use of the term. The resolution ofissue 1359 has now reintroduced the concept of an “empty” union, so there is once again the need to define it.

(See also issues 1562 and1622.)

Proposed resolution (February, 2013) [superseded]:

Change 11.5 [class.union] paragraph 2 as follows:

...At most one non-static data member of a union may have a brace-or-equal-initializer. A union is an_empty union_ if it has no non-static data members.[Note: If any...

Additional note (March, 2013):

The question was raised as to whether an example like

union A { union {}; union {}; constexpr A() {} }; A a = A();

is well-formed, which hinges on the question of whether A is an “empty union,” per 9.2.6 [dcl.constexpr] paragraph 4 bullet 5:

Must one of the empty anonymous union members be initialized forA's constructor to be constexpr?

The issue is being returned to "review" status for discussion of this point.

See also issues 1562,1587, 1621, and 1623.

Proposed resolution (August, 2013):

  1. Change 9.2.6 [dcl.constexpr] paragraph 4 as follows:

...In addition, either its function-body shall be = delete, or it shall satisfy the following constraints:

  1. Change 11.5 [class.union] paragraph 2 as follows:

A union can have member functions (including constructors and destructors), but not virtual (11.7.3 [class.virtual]) functions. A union shall not have base classes. A union shall not be used as a base class. If a union contains a non-static data member of reference type the program is ill-formed. At most one non-static data member of a union may have a brace-or-equal-initializer. [Note: If any non-static data member...

  1. Change 11.5 [class.union] paragraph 8 as follows:

A union-like class is a union or a class that has an anonymous union as a direct member. A union-like class X has a set of variant members. If X is a union its variant members are the non-static data members; otherwise, its variant members are the non-static data members of all anonymous unions that are members of X. If X is a union, a non-static data member of X that is not an anonymous union is a variant member of X. In addition, a non-static data member of an anonymous union that is a member of X is also a variant member ofX. At most one variant member of a union may have a_brace-or-equal-initializer_. [Example:

union U { int x = 0; union { }; union { int z; int y = 1; // error: initialization for second variant member of U }; };

—_end example_]

  1. Change 11.9.3 [class.base.init] paragraph 8 as follows:

In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (11.7.4 [class.abstract]), then

the entity is initialized as specified in 9.5 [dcl.init];

(This resolution also resolves issue 1562.)