Title
What is an empty union?
Status
c++14
Section
11.5 [class.union]
Submitter
Jason Merrill

Created on 2012-02-08.00:00:00 last changed 84 months ago

Messages

Date: 2013-09-15.00:00:00

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

Date: 2013-08-15.00:00:00

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:

    • ...

    • if the class is a non-empty union, or for each non-empty anonymous union member of a non-union class, exactly one non-static data member shall be initialized;

    • if the class is a union having variant members (11.5 [class.union]), exactly one of them shall be initialized;

    • if the class is a union-like class, but is not a union, for each of its anonymous union members having variant members, exactly one of them shall be initialized;

    • every constructor involved...

  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

    • if the entity is a non-static data member that has a brace-or-equal-initializer and either

      • the constructor's class is a union (11.5 [class.union]), and no other variant member of that union is designated by a mem-initializer-id or

      • the constructor's class is not a union, and, if the entity is a member of an anonymous union, no other member of that union is designated by a mem-initializer-id,

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

    • otherwise, if the entity...

(This resolution also resolves issue 1562.)

Date: 2013-03-15.00:00:00

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:

  • if the class is a non-empty union, or for each non-empty anonymous union member of a non-union class, exactly one non-static data member shall be initialized;

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.

Date: 2013-02-15.00:00:00

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...
Date: 2013-05-03.00:00:00

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

History
Date User Action Args
2014-11-24 00:00:00adminsetstatus: drwp -> c++14
2014-03-03 00:00:00adminsetstatus: dr -> drwp
2013-10-14 00:00:00adminsetmessages: + msg4691
2013-10-14 00:00:00adminsetstatus: tentatively ready -> dr
2013-09-03 00:00:00adminsetmessages: + msg4460
2013-09-03 00:00:00adminsetstatus: drafting -> tentatively ready
2013-05-03 00:00:00adminsetstatus: review -> drafting
2013-03-18 00:00:00adminsetmessages: + msg4253
2013-03-18 00:00:00adminsetmessages: + msg4252
2013-03-18 00:00:00adminsetstatus: drafting -> review
2012-02-08 00:00:00admincreate