无法从多维数据集的初始化程序推导边界 [英] Failed to deduce bounds from initializer for multi-dimensional arrays

查看:114
本文介绍了无法从多维数据集的初始化程序推导边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法编译:

int main() {
  int a[][] = { { 0, 1 },
                { 2, 3 } };
}

产生的错误消息是

error: declaration of 'a' as multidimensional array must have bounds for all dimensions except the first
int a[][] = { { 0, 1 },
           ^

这是标准指定的吗?如果是这样,那是为什么呢?我认为在这里推导界限将非常容易.

Is this specified by the standard? If so, why is that? I think deducing bounds here would be very easy.

推荐答案

这是标准指定的吗?

Is this specified by the standard?

好吧.

第8.3.4/3节,当几个数组的"规范相邻时, 创建多维数组类型;只有常数的第一个 指定数组边界的表达式可以省略.在 除了声明中对象类型不完整的声明 允许,在某些情况下,可能会省略数组绑定 函数参数的声明(8.3.5).数组绑定也可能 当声明符后跟 initializer (8.5)时,将其省略. 在这种情况下,边界是根据初始数量计算的 (8.5.1)提供的元素(例如,N)以及标识符的类型 D的是N T的数组".此外,如果前面有 在绑定范围相同的范围内声明实体 如果指定,则省略的数组范围与 较早的声明,并且类似地用于定义静态数据 班级成员.

§8.3.4/3 When several "array of" specifications are adjacent, a multidimensional array type is created; only the first of the constant expressions that specify the bounds of the arrays may be omitted. In addition to declarations in which an incomplete object type is allowed, an array bound may be omitted in some cases in the declaration of a function parameter (8.3.5). 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 of D is "array of N T". Furthermore, if there is a preceding declaration of the entity in the same scope in which the bound was specified, an omitted array bound is taken to be the same as in that earlier declaration, and similarly for the definition of a static data member of a class.

如果是这样,那为什么呢?

If so, why is that?

一方面,不能从不完整的类型(例如,void)构造数组.未知范围的数组是那些不完整的类型之一:

For one thing, an array can't be constructed from an incomplete type (void for example). An array of unknown bound is one of those incomplete types:

§8.3.4/1 ...数组类型的对象包含一个连续分配的对象 T类型的N子对象的非空集合.除下述内容外,如果 省略常量表达式,D标识符的类型 是"T的未知范围的派生的声明程序类型列表数组", 对象类型不完整. ...

§8.3.4/1 ... An object of array type contains a contiguously allocated non-empty set of N subobjects of type 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 of T", an incomplete object type. ...

§8.3.4/2可以从一种基本类型构造数组 (void除外),从指针,从指向成员的指针,从 类,枚举类型或其他数组.

§8.3.4/2 An array can be constructed from one of the fundamental types (except void), from a pointer, from a pointer to member, from a class, from an enumeration type, or from another array.

此外:

§3.9已声明但未定义的类,一个枚举 在某些情况下(7.2)键入,或者数组的大小未知或 不完整的元素类型,是一个不完全定义的对象 类型. 45 ...

§3.9 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.45 ...

45)一个未完全定义的实例的大小和布局 对象类型未知.

45) The size and layout of an instance of an incompletely-defined object type is unknown.

我认为在这里推导界限将非常容易.

I think deducing bounds here would be very easy.

初学者经常犯一个错误,那就是编译器具有神奇的力量.编译器使用它已经 拥有的信息,而不是凭空创建 信息.如果您要求它创建未知大小的对象,那么它将根本无法做到.请参见以下示例:

There is a common mistake beginners make, that the compiler has magical powers. The compiler works with information it already has, it does not create information out of thin air. If you asked it to create an object of unknown size, it simply would not be able to do so. See the following examples:

仅最里面的尺寸可以省略.中元素的大小 根据给定数组变量的类型推导一个数组.这 因此,元素的类型必须具有已知的大小.

Only the innermost dimension can be omitted. The size of elements in an array are deduced for the type given to the array variable. The type of elements must therefore have a known size.

  • char a[] = { ... };的元素(例如a[0])大小 1(8位),并且大小未知.
  • char a[6] = { ... };的元素大小为1,大小为6.
  • char a[][6] = { ... };具有大小为 6的元素(例如a[0],它是一个数组),并且大小未知.
  • char a[10][6] = { ... };的元素大小为6.大小为60.
  • char a[] = { ... }; has elements (e.g. a[0]) of size 1 (8bit), and has an unknown size.
  • char a[6] = { ... }; has elements of size 1, and has size 6.
  • char a[][6] = { ... }; has elements (e.g. a[0], which is an array) of size 6, and has an unknown size.
  • char a[10][6] = { ... }; has elements of size 6. and has size 60.

不允许:

  • char a[10][] = { ... };将包含10个大小未知的元素.
  • char a[][] = { ... };将具有未知数量的元素大小未知.
  • char a[10][] = { ... }; would have 10 elements of unknown size.
  • char a[][] = { ... }; would have an unknown number of elements of unknown size.

来源

这篇关于无法从多维数据集的初始化程序推导边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆