简体   繁体   English

为什么我们只能使用初始化列表初始化数组

[英]Why can we initialize an array only with an initializer-list

In the following example 在以下示例中

#include <iostream>

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

DEMO DEMO

Why can't we initialize an array in any way instead of with an initializer-list ? 为什么我们不能以任何方式初始化数组而不是initializer-list I tried to find it in the N4296::8.5.4 [dcl.init.list] but it seems there is no anything suitable about it. 我试图在N4296::8.5.4 [dcl.init.list]找到它,但似乎没有任何合适的东西。

This question is sort of backwards, as is the error message. 这个问题有点倒退,错误信息也是如此。 It has always been the case that you cannot initialise an array from the name of another array. 一直以来,您无法从另一个数组的名称初始化数组。 Initializer lists aren't relevant to that. 初始化列表与此无关。

[C++11: 8.5/16]: The semantics of initializers are as follows. [C++11: 8.5/16]:初始化器的语义如下。 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). 如果初始化程序是(非括号的) braced-init-list ,则对象或引用是列表初始化的(8.5.4)。
  • If the destination type is a reference type, see 8.5.3. 如果目标类型是引用类型,请参见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. 如果目标类型是字符数组, char16_t数组, char32_t数组或wchar_t数组,初始值设定项是字符串文字,请参见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. 否则,如果目标类型是数组,则程序格式错误。
  • [..] [..]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能将初始化列表用作参数? - Why can an initializer-list not be used as an argument? 通过 initializer-list 初始化一个 n 维数组 - Initialize a n-dimensional array via initializer-list 如何使用initializer-list C ++初始化数组 - How to Initialize an array using initializer-list C++ 错误C3074:只能使用初始化列表初始化数组 - error C3074: an array can only be initialized with an initializer-list 为什么这个初始化列表不能与模板参数匹配? - Why can't this initializer-list match to a template argument? C ++ 11初始化列表可以与动态数组一起使用吗? - Can a C++11 initializer-list be used with a dynamic array? 无法使用声明中的初始化列表初始化const char * / string数组的向量 - Cannot initialize a vector of const char*/string array with an initializer-list on declaration 无法初始化数组错误C2440:&#39;=&#39;:无法从&#39;initializer-list&#39;转换为&#39;abd&#39; - Cant initialize array error C2440: '=' : cannot convert from 'initializer-list' to 'abd' VC ++的一个大错误? 为什么initializer-list没有对结构进行值初始化? - A BIG bug of VC++? Why does initializer-list not value-initialize a struct? 3 个级别的初始化列表 - Initializer-list on 3 levels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM