Empty array declaration - strange compiler behavior

Viewed 2019

I've found a strange looking piece of code in a project I have to maintain. There's an empty array member of a class which doesn't lead to an compiler error. I've tested some variations of such a code with MSVC 10.0:

template<class T> struct A {
    int i[];
}; // warning C4200: nonstandard extension used : zero-sized array in struct/union

template<class T> struct B { static int i[]; };
template<class T> int B<T>::i[];

struct C {
    int i[];
}; //warning C4200: nonstandard extension used : zero-sized array in struct/union

template<class T> struct D { static int i[]; };
template<class T> int D<T>::i[4];
template<>        int D<int>::i[] = { 1 };


int main()
{
    A<void> a;
    B<void> b;
    C c;
    D<void> d0;
    D<int>  d1;

    a.i[0] = 0;     // warning C4739: reference to variable 'a' exceeds its storage space

    b.i[0] = 0;     // warning C4789: destination of memory copy is too small

    c.i[0] = 0;     // warning C4739: reference to variable 'c' exceeds its storage space

    int i[];        // error C2133: 'i' : unknown size

    d0.i[0] = 0;    // ok
    d0.i[1] = 0;    // ok

    return 0;
}

The error message at int i[] is absolutely sensible to me. The code which is shown with class D is well-formed standard C++. But what's about the classes A, B and C? What kind of types are the member variables int i[] in this classes?

2 Answers
Related