它是与未知大小的数组的定义吗? [英] Is it a definition of an array with unknow size?
问题描述
我们可以的 decalre 的一个不完整的对象类型的变量,但不能定义一个不完全类型的对象。
3.9 / 4说:
We can decalre a variable of an incomplete object type, but cannot define an object of an incomplete type. As 3.9/4 says:
这已宣告但尚未定义的类,在枚举类型
某些情况下(7.2),或未知大小或不完整的阵列的
的元素类型的,是不完全定义的对象类型。
完全确定对象类型和无效类型是不完整
类型(3.9.1)。 <青霉>物件不应被定义为具有一个不完整的的
的类型。的
A class that has been declared but not defined, an enumeration type in certain contexts (7.2), 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.
所以下面的例子
int a[];//error: storage size of 'a' isn't known
int main()
{
}
是病态的。
DEMO
但是,当我们初始化变量如下:
But, when we initialize that variable as follows:
int a[] = {1,2}; //OK
int main()
{
}
DEMO
我不明白为什么在后者的例子 int类型的[] = {1,2};
不会引发像的编译时错误一个不完全类型的对象的定义的。你能使用标准解释呢?
I don't understand why in the latter example int a[] = {1,2};
doesn't throw a compile-time error like Definition of an object of an incomplete type. Could you explain it using the Standard?
推荐答案
从N3936报价§8.3.4/ 1
Quoting from N3936 §8.3.4/1
...除以下,如果省略恒定前pression,的类型
D
的标识符为导出声明符类型列表的约束 T的未知的阵列
,一个不完整的对象类型。 ...
... Except as noted below, if the constant expression is omitted, the type of the identifier of
D
is "derived-declarator-type-list array of unknown bound ofT
", an incomplete object type. ...
除了如下面指出的是指§8.3.4/ 3
"Except as noted below" refers to §8.3.4/3
...也可在说明符后跟一个省略结合的阵列
初始化(8.5)。在这种情况下,边界被从最初的元素(比如 N
)提供的数计算
(8.5.1)和 D的标识符的类型
是的 N个阵列
T
。......
... An array bound may also be omitted when the declarator is followed by an initializer (8.5). In this case the bound is calculated from the number of initial elements (say,
N
) supplied (8.5.1), and the type of the identifier ofD
is "array ofN
T
." ...
因此,在
int a[] = {1, 2};
声明的类型 A
其实就是 INT [2]
,而不是为int []
。
这篇关于它是与未知大小的数组的定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!