std::map::operator[]

老子叫甜甜 提交于 2019-12-01 21:19:24

The statement of "using its default constructor" is confusing. More precisely, for std::map::operator[], if the key does not exist, the inserted value will be value-initialized.

When the default allocator is used, this results in the key being copy constructed from key and the mapped value being value-initialized.

For int, it means zero-initialization.

4) otherwise, the object is zero-initialized.

Map values are value-initialized by the operator[], which, for int means zero-initialization.

As defined by the standard (§23.4.4.3):

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

T() is explained as (§8.5/10):

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized ​

which means (§8.5/8): ​

To value-initialize an object of type T means:

[...]

— otherwise, the object is zero-initialized.

and zero-initialization is defined as (§8.5/6):

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

[...]

all quotes taken from n4140

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