是直接初始化禁止数组吗?

编程入门 行业动态 更新时间:2024-10-25 01:37:07
本文介绍了是直接初始化禁止数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

语言 - 律师方面,标准中的哪个条款禁止下面的代码:

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.

更多推荐

是直接初始化禁止数组吗?

本文发布于:2023-11-10 06:12:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574572.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   初始化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!