c++03: default constructor for build-in types in std::map

微笑、不失礼 提交于 2020-01-01 10:19:12

问题


I always thought that following code

std::map<int, int> test;
std::cout << test[0] << std::endl;

would print random value, because it would create unitialized value within map. However, it turns out that created int is actually always initialized to zero AND standard builtin types are also zero-initialized in certain circumstances.

The question is : when zero-initialziation is performed for standard types (int/char/float/double/size_t)? I'm pretty sure that if I declare int i; in the middle of nowhere, it will contain random data.

P.S. The question is about C++03 standard. The reason for the question is that now I'm no longer certain when I have to provide initialization for builtin types like int/float/size_t or when it can be safely omitted.


回答1:


Standard containers (map, vector, etc...) will always value-initialize their elements.

Roughly speaking, value-initialization is:

  • default-initialization if there is a default constructor
  • zero-initialization otherwise

(Some would say, the best of both worlds)

The syntax is simple: T t = T(); will value-initialize t (and T t{}; in C++11).

When you use map<K,V>::operator[], the "value" part of the pair is value-initialized, which for a built-in type yields 0.




回答2:


I'm pretty sure that if I declare int i; in the middle of nowhere, it will contain random data.

No, not always.

If you create an object of POD type, then it will be unitialized :

struct A
{
  int iv;
  float fv;
};

int main()
{
  A a; // here the iv and fv are uninitialized
}

As soon as you add a constructor, they become initialized :

struct A
{
  A(){} // iv and fv initialized to their default values
  int iv;
  float fv;
};

int main()
{
  A a; // here the iv and fv are initialized to their default values
}



回答3:


In particular for the above case :

std::map<int, int> test;
std::cout << test[0] << std::endl;

We are using std::map::operator[];

Refering http://www.cplusplus.com/reference/stl/map/operator[]/

For T& map::operator[] ( const key_type& x );

... If x does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the map size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).

So, test[0] leades to test[0] = int();

Live example here: http://ideone.com/8yYSk

Also read : std::map default value for build-in type




回答4:


please note that Data types like char,int,float are not initialized. but instead in this particular case it was initialized because it was used in stl container class.

All stl container class objects are initialized.




回答5:


The simplest solution is to simply create a template wrapper that will always be initialized.



来源:https://stackoverflow.com/questions/9481205/c03-default-constructor-for-build-in-types-in-stdmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!