std::unordered_map initialization

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 size; char *test = new char[10]; size[test] += 10; 
Is size[test] guaranteed to be 10 at the end of this sequence? asked Jan 20, 2012 at 14:53 34.1k 18 18 gold badges 127 127 silver badges 196 196 bronze badges

2 Answers 2

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;