std :: pair<>的默认构造函数设置基本类型(int,等)为零? [英] Does the default constructor of std::pair<> set basic types (int, etc) to zero?

查看:703
本文介绍了std :: pair<>的默认构造函数设置基本类型(int,等)为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写作后:

  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 is true and is_default_constructible<second_type>::value is true.
    3 Effects: Value-initializes first and second.

    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 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.

    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 value 0 (zero), taken as an integral constant expression, converted to T;
    • 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&lt;&gt;的默认构造函数设置基本类型(int,等)为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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