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

2023-01-15


1460. What is an empty union?

Section: 11.5  [class.union]     Status: C++14     Submitter: Jason Merrill     Date: 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 of issue 1359 has now reintroduced the concept of an “empty” union, so there is once again the need to define it.

(See also issues 1562 and 1622.)

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 for A'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:

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

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

  4. 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...
  5. Change 11.5 [class.union] paragraph 8 as follows:

  6. 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 of X. 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]

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

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

(This resolution also resolves issue 1562.)