是在初始化列表中没有更多元素时初始化数组的其余元素 [英] Is the rest element of array initialized when no more element in initialization list

查看:134
本文介绍了是在初始化列表中没有更多元素时初始化数组的其余元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下定义的行为是否正确?

  #include< iostream& 
using namespace std;
int main(int argc,char * argv [])
{
int a [10] = {1,2,3,4,5}
for(const auto& i:a)
cout<< i<< endl
return 0;
}

输出

  1 
2
3
4
5
0
0
0
0
0


解决方案

是,多余的元素初始化为零(整数为0,浮点数为0.0,指向 NULL )。



更准确地说,C标准要求它们被初始化,好像他们有 static 存储持续时间:



C99标准,第6.7.8.21段:


如果在括号括起来的列表中是聚合的元素或成员
,或者在字符串文字中用于初始化已知
大小的数组中的字符少于数组中的元素,则聚合的其余部分应为
默认初始化为具有静态存储持续时间的对象。


6.7.8.10:


如果具有自动存储持续时间的对象未显式初始化,则其值为
indeterminate。如果具有静态存储持续时间的对象没有被显式初始化,
则:



- 如果它有指针类型,它被初始化为一个空指针; / p>

- 如果它有算术类型,则初始化为(正或无符号)零;



是一个聚合,每个成员都根据这些规则初始化(递归);



- 如果它是一个联合,第一个成员被根据这些
规则。


Damn,它是C ++。



因此,C ++ 11中的第8.5.1.7节:



<如果T是一个(可能cv-quali fi ed)类类型(条款),则初始化类型T的对象意味着:



9)和用户提供的构造函数(12.1),则调用T的
默认构造函数(如果T没有可访问的默认
构造函数,则初始化不成功);



- 如果T是一个没有用户提供的构造函数的非联合类类型,那么对象
是零初始化的,如果T的隐式 -

- 如果T是一个数组类型,那么每个元素都是值初始化的;


$ b
$ b

8.5.1.5:


要初始化T类型的对象或引用表示:



- 如果T是标量类型(3.9),则将对象设置为值0 ,
转换为T;
103



- 如果T是一个非联合类类型(可能是cv-qualal),每个非静态数据成员和每个基类
subobject是零初始化的并且填充被初始化为零位;



- 如果T是一个(可能cv-qualifed)联合类型,对象的第一个非静态命名数据成员是零初始化,并且填充被初始化为零位;



- 如果T是数组类型,每个元素都是零初始化的;



- 如果T是引用类型,则不执行初始化。



Is the behavior below well-defined?

#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
    int a[10] = {1, 2, 3, 4, 5};
    for(const auto &i: a)
        cout << i << endl;
    return 0;
}

Output:

1
2
3
4
5
0
0
0
0
0

解决方案

Yes, excess elements are initialized to "zero" (integers to 0, floating-point numbers to 0.0, pointers to NULL).

More precisely, the C standard requires that they are initialized as if they had static storage duration:

C99 Standard, paragraph 6.7.8.21:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

6.7.8.10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules;

— if it is a union, the first named member is initialized (recursively) according to these rules.

Damn, it's C++. (Nobody except @yuan realized this, thanks!)

So paragraph 8.5.1.7 in C++11:

To value-initialize an object of type T means:

— if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

— if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.

— if T is an array type, then each element is value-initialized;

— otherwise, the object is zero-initialized.

8.5.1.5:

To zero-initialize an object or reference of type T means:

— if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T; 103

— if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;

— if T is a (possibly cv-qualified) union type, the object’s first non-static named data member is zeroinitialized and padding is initialized to zero bits;

— if T is an array type, each element is zero-initialized;

— if T is a reference type, no initialization is performed.

这篇关于是在初始化列表中没有更多元素时初始化数组的其余元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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