Search code examples
c++arraysstandardsc++17

Is it legal in C++17 to declare an array of size 0?


int main()
{
    int n[0];   
}

The code above is ok with Clang 4.0.

However, http://en.cppreference.com/w/cpp/language/array says:

The size of an array must be a value greater than zero.

Is it legal to declare an array of size 0 as per the C++17 standard?


Solution

  • Is it legal to declare an array of size 0 as per the C++17 standard?

    No, nothing has changed in C++17 to allow zero sized arrays. Per the C++17 draft [dcl.array]/1

    In a declaration T D where D has the form

    D1 [ constant-expressionopt] attribute-specifier-seqopt

    [...]If the constant-expression is present, it shall be a converted constant expression of type std​::​size_­t and its value shall be greater than zero.[...]

    emphasis mine

    What you are seeing here is a non standard compiler extension that is allowing you to compile the code.

    You can disable these extensions by using the -pedantic compiler flag.