是直接初始化禁止数组? [英] Is direct initialization forbidden for array?
问题描述
语言律师的角度来看,这些条款中禁止低于code标准:
Language-lawyer-wise, which clause in the standard forbid below code:
int arr[] (10, 42);
这将产生10个元素的数组,每个initalized 42。
This would produce an array of 10 elements, each initalized to 42.
推荐答案
语言律师明智的,8.5 / 17:
Language-lawyer wise, 8.5/17:
- 如果初始值是(非括号的)支撑,初始化列表,对象或引用列表初始化(8.5.4)
— If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).
- 如果目标类型是引用类型,见8.5.3
— If the destination type is a reference type, see 8.5.3.
- 如果目标类型是字符数组,char16_t的阵列,char32_t的阵列,或
wchar_t的阵列,并且初始化为一个字符串,见8.5.2。
— 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
一个支撑-初始化列表为{}其中任何(或没有)可以在括号内(例如 INT ARR [3] = {1,2,3}
)。考虑到这一点,没有第4个选项是可行的 INT ARR [](10,42);
,留下最后一个指示节目是非法的构造。
A braced-init-list is { } where anything (or nothing) can be inside the brackets (for example, int arr[3] = {1,2,3}
). With that in mind, none of the first 4 options are viable for int arr[] (10, 42);
, leaving the last one indicating the program is ill-formed.
这篇关于是直接初始化禁止数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!