Can you have an array data member whose bound is implied by its initializer?

35 views
Skip to first unread message

Chris Hallock

unread,
Sep 16, 2014, 7:07:49 PM9/16/14
to std-dis...@isocpp.org
Prompted by this discussion, I noticed that gcc and (I think) Clang reject the declaration of member 'y' in the following example:

int a[3]{1,2,3};      //ok
int b[]{1,2,3};       //ok

struct XY
{
   
int x[3]{1,2,3};  //ok
   
int y[]{1,2,3};   //ill-formed?
};

However, I can't find wording in the working draft that prohibits this usage. I suspect this usage was simply overlooked when NSDMIs were added. Should it be prohibited?

David Krauss

unread,
Sep 16, 2014, 7:35:14 PM9/16/14
to std-dis...@isocpp.org
On 2014–09–17, at 7:07 AM, Chris Hallock <christoph...@gmail.com> wrote:

However, I can't find wording in the working draft that prohibits this usage. I suspect this usage was simply overlooked when NSDMIs were added. Should it be prohibited?

The type of the array is incomplete, and the size is indeterminate, until it’s initialized. The initializer is not evaluated at the point of declaration but rather transplanted into the constructors. The definition, and the type of the object, is modified by the initialization. See §8.5.1/4.

There could be an explanatory note, but the current prohibition looks sufficient to me.

Richard Smith

unread,
Sep 17, 2014, 4:58:25 PM9/17/14
to std-dis...@isocpp.org
Here's the relevant wording (in [dcl.array]p3):

 "An array bound may also be omitted when the declarator is followed by an initializer (8.5). In this case the bound is calculated from the number of initial elements (say, N) supplied (8.5.1), and the type of the identifier of D is “array of N T.”"

The declaration of XY::y is *not* followed by an initializer; it's followed by a brace-or-equal-initializer, so this does not apply.

Chris Hallock

unread,
Sep 17, 2014, 5:49:55 PM9/17/14
to std-dis...@isocpp.org
The declaration of XY::y is *not* followed by an initializer; it's followed by a brace-or-equal-initializer, so this does not apply.

Ah, fair enough.
Reply all
Reply to author
Forward
0 new messages