int()返回0或任意值? [英] Does int() return 0 or an arbitrary value?

查看:143
本文介绍了int()返回0或任意值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑此代码:

template <typename T>
void f()
{T x = T();}

T = int 时, x 等于 0 到任意值?

奖金问题,因此是数组( T [N] std :: array< T,N> )这种语法可以留下任意值的内容的唯一类型。

Bonus question: and consequently, are arrays (both T[N] and std::array<T, N>) the only types where such a syntax may leave contents with arbitrary values.

推荐答案

T()提供值初始化类,联合或数组之外的类型。 (§8.5/ 7 bullet 3):否则,对象是零初始化的。对于数组,数组的每个元素都是值初始化的。

T() gives value initialization, which gives zero-initialization for a type other than a class, union or array. (§8.5/7 bullet 3): "otherwise, the object is zero-initialized." For an array, each element of the array is value initialized.

对于数组,如果是自动存储类,内容将是任意值,它是静态存储类 - 即,全局(当然,假设你不指定任何初始化)。

For an array, the content will be of an arbitrary value if it's auto storage class, but zero initialized if it's static storage class -- i.e., global (assuming, of course, that you don't specify any initialization).

这篇关于int()返回0或任意值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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