C++ Logo

std-discussion

Advanced search

[dcl.array] State explicitly that only non-empty initializers allow the array bound to be omitted (editorial?).

From: Maciej Polański <maciejpolanski75_at_[hidden]>
Date: Mon, 27 Apr 2026 19:51:34 +0200
Dear Group,

Please let me know if the world needs this fix and if it could count as
an editorial. So:

The following variable declaration is not allowed and should result in
an error:
     int arr[];
However, due to an unclear wording in The Standard, the following
statement is legal on many compilers, though UB:
     int arr[]{};
I'll be glad to be proved wrong if there's a legal use for `int arr[]{}`.

The current "state of the art" is that GCC and Clang allow a zero-sized
array if the bounds are omitted and followed by an empty initializer.
MSVC is more cautious and prevents developers from experiencing the
consequences of their errors. I suppose the authors of all the compilers
think they are compliant on this issue.
     https://godbolt.org/z/jbbcnqP9K

In my opinion, the problem lies in interpreting the unfortunate wording
of The Standard [decl.array] "9.3.4.5 Arrays - 7" without proper context:
     "An array bound may also be omitted when an object (but not a
non-static data member) of array type is initialized and the declarator
is followed by an initializer"
     https://eel.is/c++draft/dcl.decl#dcl.array-7
This paragraph does not explicitly exclude empty initializers. In the
case of arrays, "empty" means "nothing," which may result in undefined
behavior (UB).

But, in my opinion, the compiler should interpret the paragraph above in
the context of the following:
     1. "[decl.array] 9.3.4.5 Arrays - 1" That explicitly states that
the array bound "N shall be greater than zero".
     2. "[dcl.init.general] 9.5.1 General - 16.5" That describes use of
an initializer for an array and requires "1 <= i <= k," where k is the
initializer size.

In my opinion, the solution could be as simple as rewording "followed by
an initializer" to "followed by a non-empty initializer."


This issue surfaced during, not directly related to it, StackOverflow
discussion:
https://stackoverflow.com/questions/78941074/accessing-and-modifying-array-elements/78941410#78941410
Among other mistakes, the author used a zero-size array. As the
StackOverflow question showed, this mistake happens among beginners.
Perhaps some think that zero-size arrays are self-extending, as many
scripting languages have this feature. Therefore, it could be helpful to
prevent zero-size array cases.

The arguments for classifying this as an editorial are as follows:
     It is obvious that no zero-size arrays exist.
     Rule "Unclear writing, when the intention of the text in question
is well-known to the committee, but the presentation could be clearer".
     There should also be no impact on existing programs, as those using
zero-size are likely already broken.
Against:
     This removes UB, which may affect some really exotic cases. Such
changes may require formal paper.

Even though I have a hard time accepting criticism, I welcome all
opinions :)

Maciej Polański
MaciejPolanski75_at_[hidden]

Received on 2026-04-27 17:51:38