Title
Naming function members of anonymous unions
Status
review
Section
7.5.5.1 [expr.prim.id.general]
Submitter
Hubert Tong

Created on 2025-11-11.00:00:00 last changed 2 weeks ago

Messages

Date: 2025-11-22.13:36:11

Possible resolution:

  1. Change in 7.5.5.1 [expr.prim.id.general] bullet 3.1 as follows:

    If an id-expression E denotes a variant member M of an anonymous union (11.5.2 [class.union.anon]) U:
    • ...
    [Note 3: Under this interpretation, E no longer denotes a non-static data member. —end note] [Example 3: N::x is interpreted as N::u.x, where u names the anonymous union variable. —end example]
  2. Change in 11.4.5.2 [class.default.ctor] paragraph 1 as follows:

    ... If there is no user-declared constructor or constructor template for class X and X is not an anonymous union, a non-explicit constructor having no parameters is implicitly declared as defaulted (9.6 [dcl.fct.def]). An implicitly-declared default constructor is an inline public member of its class.
  3. Change in 11.4.5.3 [class.copy.ctor] paragraph 6 as follows:

    If the class definition does not explicitly declare a copy constructor and the class is not an anonymous union, a non-explicit one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defaulted (9.6 [dcl.fct.def]). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor (D.6 [depr.impldec]).
  4. Change in 11.4.5.3 [class.copy.ctor] paragraph 8 as follows:

    If the definition of a class X does not explicitly declare a move constructor, a non-explicit one will be implicitly declared as defaulted if and only if
    • X is not an anonymous union,
    • X does not have a user-declared copy constructor,
    • X does not have a user-declared copy assignment operator,
    • X does not have a user-declared move assignment operator, and
    • X does not have a user-declared destructor.
  5. Change in 11.4.6 [class.copy.assign] paragraph 2 as follows:

    If the class definition does not explicitly declare a copy assignment operator and the class is not an anonymous union, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator is defined as deleted; otherwise, it is defaulted (9.6 [dcl.fct.def]). The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor (D.6 [depr.impldec]). ...
  6. Change in 11.4.6 [class.copy.assign] paragraph 4 as follows:

    If the definition of a class X does not explicitly declare a move assignment operator, one will be implicitly declared as defaulted if and only if
    • X is not an anonymous union,
    • X does not have a user-declared copy constructor,
    • X does not have a user-declared move constructor,
    • X does not have a user-declared copy assignment operator, and
    • X does not have a user-declared destructor.
  7. Change in 11.4.7 [class.dtor] paragraph 2 as follows:

    If a class has no user-declared prospective destructor and the class is not an anonymous union, a prospective destructor is implicitly declared as defaulted (9.6 [dcl.fct.def]). An implicitly-declared prospective destructor is an inline public member of its class.
  8. Change in 11.4.7 [class.dtor] paragraph 4 as follows:

    At the end of the definition of a class other than an anonymous union, overload resolution is performed among the prospective destructors declared in that class with an empty argument list to select the destructor for the class, also known as the selected destructor. The program is ill-formed if overload resolution fails. Destructor selection does not constitute a reference to, or odr-use (6.3 [basic.def.odr]) of, the selected destructor, and in particular, the selected destructor may be deleted (9.6.3 [dcl.fct.def.delete]).
  9. Change in 11.5.2 [class.union.anon] paragraph 1 as follows:

    A union of the form
      union { member-specification } ;
    
    is called an anonymous union; it defines an unnamed type and an unnamed object of that type called an anonymous union member if it is a non-static data member or an anonymous union variable otherwise. All objects of such an unnamed type shall be such an unnamed object. Each member-declaration in the member-specification of an anonymous union shall either define one or more public non-static data members or be a static_assert-declaration. Nested types, anonymous unions, and functions shall not be declared within an anonymous union. The names of the members of an anonymous union are bound in the scope inhabited by the union declaration.
Date: 2025-11-22.13:36:11

CWG 2025-11-21

Additional discussion has shown many issues arising from the ability to designate anonymous unions via reflection, inspect its implicitly-declared special member functions, and create complete objects of anonymous union type. Not declaring those special member functions in the first place resolves most of these concerns; users cannot declare member functions of anonymous unions anyway. CWG is interested in adding a type trait (both at the type and reflection levels) that identifies anonymous unions.

Date: 2025-11-22.13:36:11

Possible resolution [SUPERSEDED]:

Change in 7.5.5.1 [expr.prim.id.general] bullet 3.1 as follows:

If an id-expression E denotes a variant member M of an anonymous union (11.5.2 [class.union.anon]) U:
  • ...
[Note 3: Under this interpretation, E no longer denotes a non-static data member. —end note] [Example 3: N::x is interpreted as N::u.x, where u names the anonymous union variable. —end example]
Date: 2025-11-11.00:00:00

(From submission #802.)

Subclause 7.5.5.1 [expr.prim.id.general] paragraph 3 does not properly handle an id-expression that denotes a function member of a namespace-scope anonymous union. With reflection, those can be named.

History
Date User Action Args
2025-11-22 13:36:11adminsetmessages: + msg8412
2025-11-22 13:36:11adminsetmessages: + msg8411
2025-11-22 13:36:11adminsetstatus: open -> review
2025-11-19 20:51:31adminsetmessages: + msg8404
2025-11-11 00:00:00admincreate