问题
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?
回答1:
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;
回答2:
What's the difference? Value-initialization for class-type objects entails default construction, so the answer is "both". For a map <K, V>
, the new object will be initialized with V()
.
All standard containers initialize new elements with value- or direct-initialization (the latter possibly through a copy construction). It is not possible for new standard container elements to be in an "uninitialized" state (i.e. there is no mechanism that default-initializes elements).
来源:https://stackoverflow.com/questions/8943261/stdunordered-map-initialization