std :: pair<>的默认构造函数设置基本类型(int,等)为零? [英] Does the default constructor of std::pair<> set basic types (int, etc) to zero?
问题描述
写作后:
std :: pair< int,int& X;
我保证x.first和x.second都为零吗?还是他们有什么价值?
我关心的原因是因为我试图确定其值是指针的映射是否保证在访问不在映射中的元素时返回NULL 。即,如果我这样做:
std :: map< int,void *> my_map;
std :: cout<< int(my_map [5])< std :: endl;
那么我保证得到零(NULL)?
是的,这种保证是正确的。引用C ++ 11标准,§20.3.2/ 2-3:
  ;
constexpr pair();
2 需要:
is_default_constructible< first_type> :: value
是true
和is_default_constructible< second_type> :: value
是true
。
3 效果:首先c $ c>和
第二
。
/ p>
要 value-initialize 类型
T
表示:
- 如果
T
是(可能是cv限定的)类类型一个用户提供的构造函数,则调用T
的默认构造函数(如果T
没有可访问的默认构造函数);
- 如果
T
是没有用户提供的构造函数的(可能是cv限定的)非联合类类型,零初始化,如果T
的隐式声明的默认构造函数是非平凡的,那么构造函数被调用。
- 如果
T
是数组类型,则每个元素都被初始化;
b
最后,§8.5/ 5:
要零初始化对象或引用类型
T
表示:
- if
T
是标量类型,对象设置为值0
(零),作为一个整数常数表达式,转换为T
;
- 如果
T
是(可能是cv限定的)非联合类类型,每个非静态数据成员和每个base-类子对象被零初始化,并且填充被初始化为零位;
- 如果
T
是(可能是cv限定的)联合类型,对象的第一个非静态命名数据成员是零初始化并且填充被初始化为零比特;
- 如果
T
是数组类型,则每个元素都是初始化的;
- 如果
T
是参考类型,则不执行初始化。
After writing:
std::pair<int, int> x;
Am I guaranteed that x.first and x.second are both zero? Or could they have any value?
The reason why I care is because I'm trying to determine whether a map whose values are pointers is guaranteed to return NULL if I access an element that's not in the map. I.e., if I do:
std::map<int, void*> my_map; std::cout << int(my_map[5]) << std::endl;
then am I guaranteed to get zero (NULL)? Or is the behavior undefined?
解决方案Yes, that guarantee holds true. Quoting the C++11 standard, §20.3.2/2-3:
constexpr pair();
2 Requires:
is_default_constructible<first_type>::value
istrue
andis_default_constructible<second_type>::value
istrue
.
3 Effects: Value-initializesfirst
andsecond
.And §8.5/7:
To value-initialize an object of type
T
means:
- if
T
is a (possibly cv-qualified) class type with a user-provided constructor, then the default constructor forT
is called (and the initialization is ill-formed ifT
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, ifT
'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.
And lastly, §8.5/5:
To zero-initialize an object or reference of type
T
means:
- if
T
is a scalar type, the object is set to the value0
(zero), taken as an integral constant expression, converted toT
;- 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 zero-initialized 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.
这篇关于std :: pair<>的默认构造函数设置基本类型(int,等)为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!