Title
Kind of initialization for a designated-initializer-list
Status
c++23
Section
9.4.2 [dcl.init.aggr]
Submitter
Jim X

Created on 2022-07-13.00:00:00 last changed 16 months ago

Messages

Date: 2022-11-20.07:54:16

Proposed resolution (approved by CWG 2022-09-23):

  1. Change in 9.4.1 [dcl.init.general] paragraph 14 as follows:

    The initialization that occurs in the = form of a brace-or-equal-initializer or condition (8.5 [stmt.select]), as well as in argument passing, function return, throwing an exception (14.2 [except.throw]), handling an exception (14.4 [except.handle]), and aggregate member initialization other than by a designated-initializer-clause (9.4.2 [dcl.init.aggr]), is called copy-initialization.
  2. Change in 9.4.2 [dcl.init.aggr] bullet 4.1 as follows:

    If the element is an anonymous union member and the initializer list is a brace-enclosed designated-initializer-list, the element is initialized by the designated-initializer-list braced-init-list { D }, where D is the designated-initializer-clause naming a member of the anonymous union member.
  3. Change in 9.4.2 [dcl.init.aggr] bullet 4.2 as follows:

    Otherwise, the element is copy-initialized from the corresponding initializer-clause or is initialized with the brace-or-equal-initializer of the corresponding designated-initializer-clause. If that initializer is of the form assignment-expression or = assignment-expression and a narrowing conversion (9.4.5 [dcl.init.list]) is required to convert the expression, the program is ill-formed. [ Note: If the initialization is by designated-initializer-clause, its form determines whether copy-initialization or direct-initialization is performed.-- end note]
Date: 2022-11-20.07:54:16

CWG telecon 2022-09-09:

The examples #1 to #4 should all be valid, direct-initializing the s member.

Date: 2022-11-20.07:54:16

Suggested resolution [SUPERSEDED]:

  1. Change in 9.4.2 [dcl.init.aggr] bullet 4.1 as follows:

    If the element is an anonymous union member and the initializer list is a brace-enclosed designated-initializer-list, the element is initialized by the designated-initializer-list braced-init-list { D }, where D is the designated-initializer-clause naming a member of the anonymous union member.
  2. Change in 9.4.2 [dcl.init.aggr] bullet 4.2 as follows:

    Otherwise, the element is copy-initialized from the corresponding initializer-clause or is initialized copy-initialized or direct-initialized with the brace-or-equal-initializer of the corresponding designated-initializer-clause, according to the form of the brace-or-equal-initializer (9.4.1 [dcl.init.general]). ...
Date: 2022-11-15.00:00:00

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

Consider:

struct S {
  explicit S(int){}
};
struct A {
  S s;
};
struct B {
  union {
    S s;
  };
};
int main() {
  A a1 = {.s{0}};  // #1
  A a2{.s{0}};     // #2
  B b1 = {.s{0}};  // #3
  B b2{.s{0}};     // #4
}

Subclause 9.4.2 [dcl.init.aggr] bullet 4.2 specifies:

Otherwise, the element is copy-initialized from the corresponding initializer-clause or is initialized with the brace-or-equal-initializer of the corresponding designated-initializer-clause.

It is unclear what kind of initialization is performed for "is initialized". For example, one could imagine that the top-level kind of initialization is inherited. On the other hand, 9.4.1 [dcl.init.general] paragraph 14 specifies:

The initialization that occurs in the = form of a brace-or-equal-initializer or condition (8.5 [stmt.select]), as well as in argument passing, function return, throwing an exception (14.2 [except.throw]), handling an exception (14.4 [except.handle]), and aggregate member initialization (9.4.2 [dcl.init.aggr]), is called copy-initialization.

There is implementation divergence: gcc and icc reject the example; clang and MSVC accept.

History
Date User Action Args
2023-07-16 13:00:43adminsetstatus: open -> c++23
2023-07-16 13:00:43adminsetstatus: drwp -> open
2023-02-18 18:43:04adminsetstatus: dr -> drwp
2022-11-25 05:14:04adminsetstatus: ready -> dr
2022-11-20 07:54:16adminsetmessages: + msg7052
2022-09-24 20:32:25adminsetmessages: + msg6946
2022-09-24 20:32:25adminsetstatus: drafting -> ready
2022-09-09 20:47:33adminsetstatus: open -> drafting
2022-08-28 07:13:02adminsetmessages: + msg6915
2022-07-13 00:00:00admincreate