C++ Logo

std-discussion

Advanced search

The wording about brace elision for aggregate initialization is not precise

From: jim x <xmh970252187_at_[hidden]>
Date: Fri, 27 Nov 2020 22:26:22 +0800
I have some issues with the wording of brace elision in section [
dcl.init.aggr] all along. The relevant rules are:

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; it is erroneous for there to be more initializer-clauses than
elements. 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; any remaining
initializer-clauses are left to initialize the next element of the
aggregate of which the current subaggregate is an element.

I don't think the term ` initializer-list ` is an appropriate wording in
this rule. Please consider the following example
````
struct A{
  int a;
};
struct B{
  int b;
  int c;
};
struct Data{
  A a;
   B b;
};
Data data = {{0},1,2};
````
According to the grammar for braced-init-list, its definition is:
braced-init-list
{ initializer-list ,opt }
{}

initializer-list:
initializer-clause ...opt
initializer-list , initializer-clause ...opt

The relevant wording for braced-init-list is:
*List-initialization*
<https://timsong-cpp.github.io/cppwp/n4659/dcl.init.list#def:list-initialization>
is
initialization of an object or reference from a *braced-init-list
<https://timsong-cpp.github.io/cppwp/n4659/dcl.init#nt:braced-init-list>*.
Such an initializer is called an *initializer list*
<https://timsong-cpp.github.io/cppwp/n4659/dcl.init.list#def:initializer_list>,
and the comma-separated *initializer-clause
<https://timsong-cpp.github.io/cppwp/n4659/dcl.init#nt:initializer-clause>*s of
the list are called the *elements*
<https://timsong-cpp.github.io/cppwp/n4659/dcl.init.list#def:elements> of
the initializer list.

So, according to the definition for braced-init-list, its
initializer-list are " {0},1,2" (per the grammar), in other words, the
braced-init-list has three * initializer-clauses, its *initializer-list
consists of these three initializer-clauses.
The first initializer-clause is used to initialize the subaggregate `a` of
the `data`(although it begins with left brace, but it can't be called the
initializer-list of " {{0},1,2} "), and the remaining initializer-clauses
are used to initialize the elements of the subaggregate `b` of the
`data`(they're also can not be called the initializer-list of
"{{0},1,2}").

The wording `initializer-list` in [dcl.init.aggr#15] may be misleading. I
think it should use ` initializer-clause` instead. Does anyone have the
same opinion as me?

Received on 2020-11-27 08:26:35