多维数组缺少大括号
Missing braces for multi-dimensional arrays
当我为 Visual Studio 中的 Android 共享库构建项目时,我在下面收到一条警告消息。
warning : suggest braces around initialization of subobject
[-Wmissing-braces]
此消息表示仅使用一对大括号的数组初始化语句。
int myArray[ROW][COL] = { 1, 2, 3, 4, 5, 6, ..., 451, 452, 453 };
我不能写两对的原因是将来有可能改变 ROW 和 COL 的大小。
它工作正常,但我不确定是否可以这样离开项目,因为我在为 Windows 仅应用程序编写代码时从未见过这样的警告消息。
我必须认真对待吗?
你所拥有的称为通过大括号省略进行聚合初始化,你完全没问题,代码符合标准。
If the aggregate initialization uses the form with the equal sign (T a
= {args..}), (until C++14) the braces around the nested initializer lists may be elided (omitted), in which case as many initializer
clauses as necessary are used to initialize every member or element of
the corresponding subaggregate, and the subsequent initializer clauses
are used to initialize the following members of the object. However,
if the object has a sub-aggregate without any members (an empty
struct, or a struct holding only static members), brace elision is not
allowed, and an empty nested list {} must be used.
查看更多详情here and here。
当我为 Visual Studio 中的 Android 共享库构建项目时,我在下面收到一条警告消息。
warning : suggest braces around initialization of subobject
[-Wmissing-braces]
此消息表示仅使用一对大括号的数组初始化语句。
int myArray[ROW][COL] = { 1, 2, 3, 4, 5, 6, ..., 451, 452, 453 };
我不能写两对的原因是将来有可能改变 ROW 和 COL 的大小。
它工作正常,但我不确定是否可以这样离开项目,因为我在为 Windows 仅应用程序编写代码时从未见过这样的警告消息。
我必须认真对待吗?
你所拥有的称为通过大括号省略进行聚合初始化,你完全没问题,代码符合标准。
If the aggregate initialization uses the form with the equal sign (T a = {args..}), (until C++14) the braces around the nested initializer lists may be elided (omitted), in which case as many initializer clauses as necessary are used to initialize every member or element of the corresponding subaggregate, and the subsequent initializer clauses are used to initialize the following members of the object. However, if the object has a sub-aggregate without any members (an empty struct, or a struct holding only static members), brace elision is not allowed, and an empty nested list {} must be used.
查看更多详情here and here。