语言 - 律师方面,标准中的哪个条款禁止下面的代码:
int arr [] );这将产生一个10个元素的数组,每个元素初始化为42。
解决方案语言律师聪明,8.5 / 17:
<如果初始化器是(非圆括号)支撑初始化列表,则对象或引用被列表初始化(8.5.4)。- 如果目标类型是一个引用类型,见8.5.3。
- 如果目标类型是一个字符数组,一个char16_t数组,一个char32_t数组或一个数组的wchar_t,初始化程序是一个字符串文字,见8.5.2。
- 如果初始化程序是(),对象是初始化的。 / p>
- 否则,如果目标类型是数组,程序就是错误的。
braced-init-list是{},其中可以在括号中包含任何内容(例如, int arr [3] = {1,2,3} )。考虑到这一点,前面4个选项中没有一个可行于 int arr [](10,42); ,留下最后一个表示程序未成形。
Language-lawyer-wise, which clause in the standard forbid below code:
int arr[] (10, 42);This would produce an array of 10 elements, each initalized to 42.
解决方案Language-lawyer wise, 8.5/17:
— 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
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.
更多推荐
是直接初始化禁止数组吗?
发布评论