是直接初始化禁止数组吗? [英] Is direct initialization forbidden for array?

查看:181
本文介绍了是直接初始化禁止数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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.

这篇关于是直接初始化禁止数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆