- 论坛徽章:
- 0
|
回复 8# linuxwyc
起先我也觉得奇怪,char arr[];这样的成员声明能通过吗?看lz的代码似乎是通过了,所以也就算了。
后来查了一下,数组声明不指定bound,那么该数组的类型是incomplete type。C++白皮书3.9说:
A class that has been declared but not defined, or an array of unknown size or of incomplete element type, is an incompletely-defined object type. Incompletely-defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.
也就是说这样的成员声明是不符合标准的。但是实际使用的时候,gcc和vc都支持这种写法,并且自动指定bound为0,也就是等效于char arr[0];,因为效果一样,而且vc的警告都是“warning C4200: 使用了非标准扩展 : 结构/联合中的零大小数组”。但是再参看白皮书8.3.4说:
In a declaration T D where D has the form
D1 [constant-expression opt]
... If the constant-expression(5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.
所以bound为0也不符合标准,所以vc给警告,但是允许。
而对于这种object with incomplete type,标准不允许,更不会说size是多少,所以他们的sizeof是unspecified的,我试了一下,vc下给的是1,和空结构一样,gcc给0,因为它支持这种用法,而且就是为了达到数组的越界或内存的重叠的目的。 |
评分
-
查看全部评分
|