Title
Implicitly invoking the deleted destructor of an anonymous union member
Status
drwp
Section
11.4.7 [class.dtor]
Submitter
Corentin Jabot

Created on 2023-07-11.00:00:00 last changed 6 months ago

Messages

Date: 2023-08-26.21:49:54

Proposed resolution (approved by CWG 2023-08-25):

  1. Change in 9.4.2 [dcl.init.aggr] paragraph 9 as follows:

    The destructor for each element of class type other than an anonymous union member is potentially invoked (11.4.7 [class.dtor]) from the context where the aggregate initialization occurs
  2. Change in 11.4.7 [class.dtor] paragraph 13 as follows:

    After executing the body of the destructor and destroying any objects with automatic storage duration allocated within the body, a destructor for class X calls the destructors for X's direct non-variant non-static data members other than anonymous unions, the destructors for X's non-virtual direct base classes and, if X is the most derived class (11.9.3 [class.base.init]), its destructor calls the destructors for X's virtual base classes. All destructors are called as if they were referenced with a qualified name, that is, ignoring any possible virtual overriding destructors in more derived classes. Bases and members are destroyed in the reverse order of the completion of their constructor (see 11.9.3 [class.base.init]).
  3. Change in 14.3 [except.ctor] paragraph 3 as follows:

    A subobject is known to be initialized if it is not an anonymous union member and its initialization is specified
    • in 11.9.3 [class.base.init] for initialization by constructor,
    • in 11.4.5.3 [class.copy.ctor] for initialization by defaulted copy/move constructor,
    • in 11.9.4 [class.inhctor.init] for initialization by inherited constructor,
    • in 9.4.2 [dcl.init.aggr] for aggregate initialization,
    • in 7.5.6.3 [expr.prim.lambda.capture] for the initialization of the closure object when evaluating a lambda-expression,
    • in 9.4.1 [dcl.init.general] for default-initialization, value-initialization, or direct-initialization of an array.
Date: 2023-11-15.00:00:00

[Accepted as a DR at the November, 2023 meeting.]

Consider:

  struct S{
    ~S() {}
  };

  struct A {
    union {
      S arr_;
    };
    ~A(); // user-provided!
  };

  auto foo() {
    return A{S()};
  }

Does the destructor of A attempt to destroy the (unnamed) data member that is the anonymous union? The latter has a deleted destructor per 11.4.7 [class.dtor]. For the default constructor, 11.9.3 [class.base.init] paragraph 9.2 prevents the corresponding construction.

History
Date User Action Args
2024-04-05 21:43:46adminsetstatus: dr -> drwp
2023-12-19 10:15:28adminsetstatus: ready -> dr
2023-11-10 14:27:11adminsetstatus: tentatively ready -> ready
2023-08-26 21:49:54adminsetmessages: + msg7403
2023-08-26 21:49:54adminsetstatus: open -> tentatively ready
2023-07-11 00:00:00admincreate