C++ Quiz94 of 173
Difficulty:
⬅️ Prev: 206Quiz ID: 207Next: 208 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>
#include <map>

int main() {
    std::map<int, int> m;
    std::cout << m[42];
}
Hint

What does the [] operator do if the key is not present?

Reveal answer

0

Explanation

The [] operator inserts an element if the key is not present. For a simple int, it inserts 0.

According to §[map.access]¶2 in the standard:

mapped_type& operator[](key_type&& x);

Effects: Equivalent to: return try_emplace(move(x)).first->second;

where try_emplace is defined by §[map.modifiers]¶4:

template<class... Args>
 pair<iterator, bool> try_emplace(const key_type& k, Args&&... args);
template<class... Args>
 iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args);

Effects: If the map already contains an element whose key is equivalent to k, there is no effect. Otherwise inserts an object of type value_type constructed with piecewise_construct, forward_as_tuple(k), forward_as_tuple(std::forward(args)...).

value_type is just a typedef for pair, which in our case is pair. So it inserts a pair(42, int()).

int() is a value-initialization, which for non-class, non-array types means zero-initialization (see §[dcl.init]¶8). And according to §[dcl.init.general]¶6.1:

To zero-initialize an object or reference of type T means:

— if T is a scalar type (§[basic.types.general]), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T;

So setting the value for the key 42 to T() means setting it to 0.

⬅️ Prev: 206Quiz ID: 207Next: 208 ➡️