Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the definition of the brace elision of aggregate initialization #5353

Open
xmh0511 opened this issue Mar 20, 2022 · 1 comment
Open

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Mar 20, 2022

[dcl.init.aggr] p16 state

Braces can be elided in an initializer-list as follows. If the initializer-list begins with a left brace, then the succeeding comma-separated list of initializer-clauses initializes the elements of a subaggregate;

Presumably, the initializer-list refers to the initializer-list enclosed in the brace-list-initializer of the aggregate initialization. Consider this example:

struct A{
   int a;
};
struct B{
   int arr[2];
   A obj;
};
int main(){
   B b = {0,1,{2}}; // #1
   B b2 = {{0,1},2}; //#2
}

In this case, the initializer-list at #1 is 0,1,{2} that does not begin with a left brace.

If, however, the initializer-list for a subaggregate does not begin with a left brace, then only enough initializer-clauses from the list are taken to initialize the elements of the subaggregate;

The initializer-list at #2 is {0,1}, 2 that does begin with a left brace, however, we want the brace elision rule can apply to the initializer-clause 2 when initializing the subaggregate B::obj. In such two cases, we just intend to mean whether the initializer-clause begins with a left brace or not.

Maybe, change [dcl.init.aggr] p16 to

Braces can be elided in an initializer-list as follows. If the initializer-clause in the initializer-list that would initialize the subaggregate element is not a braced-init-list, only enough initializer-clauses from the list are taken to initialize the elements of the subaggregate; the elements are considered to be explicitly initialized. Any remaining initializer-clauses(if any) are left to initialize the next element of the aggregate of which the current subaggregate is an element.

if the initializer-clause is a brace-init-list, I think it is not necessary to specify in this paragraph. That would be redundant since it is just a normal aggregate initialization.

The emphasized wording may be more precise if it is changed to

If the initializer-clause in the initializer-list that would initialize the subaggregate element is not a braced-init-list, only enough initializer-clauses from the list are taken with comma-separation to form an initializer-list D, the subaggregate is initialized by braced-init-list { D }

@frederick-vs-ja
Copy link
Contributor

It seems that P3106R0 will also resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants