为什么我在cpp的初始化中指定多维数组时必须具有边界 [英] why multidimensional array must have bounds when I specify it in the initialization in cpp

查看:86
本文介绍了为什么我在cpp的初始化中指定多维数组时必须具有边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试像这样初始化多维数组时:

  int a [] [] = {{1,2,3},{4,5,6}}; 

我收到此错误:

 错误:将iArray声明为多维数组时,除第一个数组外,所有维都必须具有边界 

但是我想了解为什么,由于{},编译器应该知道它是一个[2] [3]数组.

我知道也可以这样做:

  int a [] [3] = {1,2,3,4,5,6}; 

在那种情况下,确实编译器无法猜测第二维是否丢失,但是为什么在第一种情况下不允许使用a [] []?

解决方案

[耸肩]就是这样.

您可以建议更改标准以允许这样做.您需要:

  • 编写规范
  • 解释为什么这值得增加额外的复杂性(在标准和实现上).
  • 可能会修改现有的实现,以将其添加为扩展.

对我来说,这不算很多,但另一方面,也没有什么好处(增加对原始数组的额外支持可能在每个人的脑海中都是不足的)./p>

when trying to initialize a multidimensional array like this:

int a[][] = { {1,2,3},
              {4,5,6} };

I get this error:

error: declaration of iArray as multidimensional array must have bounds for all dimensions except the first

but I want to understand why, the compiler should know it's a[2][3] array because of the {}.

I know that is allowed also to do:

int a[][3] = {1,2,3,4,5,6};

and for that case indeed the compiler can't guess what is the 2nd dimension if it missing, but why not to allow the use of a[][] in the first case?

解决方案

[Shrug] That's just the way it is.

You could propose changing the standard to allow this. You would need to:

  • write a specification
  • explain why this is worth adding additional complexity (to both standard and implementations).
  • probably modify an existing implementation to add this as an extension.

It's not obvious to me that this would have many gotcha's, but on the other hand, it's also not obvious how much of a benefit it would be (adding extra support for raw arrays is probably pretty low on everyone's mind).

这篇关于为什么我在cpp的初始化中指定多维数组时必须具有边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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