Dynamic Zero-Length Arrays in C++ Dynamic Zero-Length Arrays in C++ arrays arrays

Dynamic Zero-Length Arrays in C++


With parenthesis you have a regular type-id, instead of the special syntax called a new-type-id that supports dynamic array size.

As of C++17 the standard makes no special provision for this use of a type-id, so the question boils down to whether you can write

auto main() -> int{    using Argh = int[0];}

You can't, because a type-id's type is defined in terms of a fictitious “declaration for a variable or function of that type that omits the name of the entity” (C++17 §11.1/1), and for declaration of an array variable the rule is “If the constant-expression (8.20) is present, it shall be a converted constant expression of type std::size_t and its value shall be greater than zero” (C++17 §11.3.4/1).


Now there's a fair bit of interpretation in this. E.g. without such reasonable interpretation the last quote would not reasonably say that the array size must be non-negative and representable as a size_t. Instead, with no reasonable interpretation, it would literally say that a declaration such as

int x[42];

is invalid (of course it isn't), and would have to be expressed as

int x[std::size_t(42)];

Determining what is a reasonable interpretation or not used to be easy. One could just ask, does it make sense? So for the case above the answer would be no, and one could ditch that possibility.

However, to some degree with C++14 and increasingly more so with C++17 I find that that earlier dependable technique fails. Since the question at hand is about C++03 functionality, however, I think you can trust this answer. But if it were a question about C++14 or later stuff, then do keep in mind that any apparently clear-cut answer probably involves some subjective interpretation that probably can't be resolved by asking whether it makes sense or not.


No, the zero-size case cannot use the parenthesized type-id. The behavior for an array of size 0 is given (in the current draft) only for the expression in a noptr-new-declarator ([expr.new]/7). A new with a parenthesized type attempts to create an object of that type, and there are no arrays of size 0 ([dcl.array]/1), not even as a type-id ([dcl.name]/1).

Zero-sized arrays are of course a common extension, so practical results may vary.