Skip to content

[dcl.init.aggr] Brace elision is incorrectly defined for union subaggregates #748

@cpplearner

Description

@cpplearner

Full name of submitter (unless configured in github; will be published with the issue): Tam S. B.

Reference (section label): [dcl.init.aggr]

Link to reflector thread (if any):

Issue description:

Consider:

union A {
    const char* x;
    int y;
};

struct B {
    A a;
    int t;
};

B b = {nullptr, 1};

[dcl.init.aggr]/14:

Otherwise, the aggregate element is an aggregate and that subaggregate is replaced in the list of aggregate elements by the sequence of its own aggregate elements, and the appertainment analysis resumes with the first such element and the same initializer-clause.

Per [dcl.init.aggr]/14, to determine the appertainment of each initializer-clause in B b = {nullptr, 1}, the sequence {nullptr, 1} is compared with the list of aggregate elements {B::a, B::t}. Since nullptr cannot appertain to B::a of type A, the latter is replaced by A's list of aggregate elements, turning the list to {A::x, A::y, B::t}. This renders the initialization equivalent to B b = {.a = {.x = nullptr, .y = 1}}, which is ill-formed per [dcl.init.aggr]/20.

I believe that this initialization should be well-formed and equivalent to B b = {.a = {.x = nullptr}, .t = 1};.

Suggested resolution:

Modify [dcl.init.aggr]/14 as indicated:

Otherwise, the aggregate element is an aggregate and that subaggregate is replaced in the list of aggregate elements by the sequence of its own aggregate elements if it is not an union, and by its first aggregate element otherwise. , and tThe appertainment analysis resumes with the first such element and the same initializer-clause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions