我在一个项目中发现了一段奇怪的代码,我必须维护它。有一个类的空数组成员,不会导致编译器错误。我测试过的这样的一些代码的变化与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;
}
错误消息在int i[]
绝对是明智的我。与类D
一起显示的代码是格式良好的标准C++。但是关于类A
,B
和C
?这个类中的成员变量int i[]
是什么类型的?
问题是:为什么警告(与类'A','B'和'C'有关)警告而不是错误?在我看来,这与我在局部变量声明中得到的错误相比是不对称的。 – 0xbadf00d
请参阅我的编辑。 – sergio
谢谢,另一个“漂亮”的微软扩展到C++标准...... – 0xbadf00d