std::unordered_map initialization

Suma picture Suma · Jan 20, 2012 · Viewed 12k times · Source

When I access an element in std::unordered_map using operator [] for the first time, it is automatically created. What (if any) are guarantees about its initialization? (It is guaranteed to be value initialized, or only to be constructed)?

Example:

std::unordered_map<void *, size_t> size;
char *test = new char[10];
size[test] += 10;

Is size[test] guaranteed to be 10 at the end of this sequence?

Answer

NPE picture NPE · Jan 20, 2012

Is size[test] guaranteed to be 10 at the end of this sequence?

Yes. In the last line of your code, size[test] value-initializes the element to T(), or in this case size_t():

C++11 23.4.4.3 map element access [map.access]

T& operator[](const key_type& x);

1 Effects: If there is no key equivalent to x in the map, inserts value_type(x, T()) into the map.

As to T(), the exact language is a somewhat involved, so I'll try to quote the relevant bits:

C++11 8.5.16 The semantics of initializers are as follows.

If the initializer is (), the object is value-initialized.


8.5.7 To value-initialize an object of type T means:

— if T is a (possibly cv-qualified) class type ...

— if T is a (possibly cv-qualified) non-union class type ...

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

otherwise, the object is zero-initialized.


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