c++ - 为什么我们只能用初始化列表初始化一个数组

标签 c++ arrays

在下面的例子中

#include <iostream>

int a[][2] = {{1, 4}, {2, 6}};
int b[][3] = a; // error: array initializer must be an initializer list

DEMO

为什么我们不能以任何方式初始化数组而不是使用initializer-list?我试图在 N4296::8.5.4 [dcl.init.list] 中找到它,但似乎没有任何合适的东西。

最佳答案

这个问题有点倒退,错误消息也是如此。 总是您不能从另一个数组的名称初始化一个数组。初始化列表与此无关。

[C++11: 8.5/16]: The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.

  • If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).
  • If the destination type is a reference type, see 8.5.3.
  • If the destination type is an array of characters, an array of char16_t, an array of char32_t, or an array of wchar_t, and the initializer is a string literal, see 8.5.2.
  • If the initializer is (), the object is value-initialized.
  • Otherwise, if the destination type is an array, the program is ill-formed.
  • [..]

关于c++ - 为什么我们只能用初始化列表初始化一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27752546/

相关文章:

c# - 将字节数组数据映射到 C# 中的结构

c++ - 使用尚未定义的参数的 unique_ptr 实例化不会导致错误

c++ - 避免具有多个目标的重复 CMake 代码

c - 将函数指针表的索引与表内容同步

php - 使用 PHP 将数组数据值插入 MySQL 数据库表

c++ - 排序a、b、c是否等价于排序c;对 b 进行排序;排序一个?

c++ - C++17 中有 typeid 的反函数吗?

JavaScript 乘法表数组

arrays - 使用 gdb 时,如何在 Pascal 中打印数组中的部分值

c - 初始化二维数组然后转置它