We know that if we try to access a nonexistent key of std::map with the operator [] , the function will insert a new element with that key.
We have: std::map<std::string, bool> map_xxx;
Is it guaranteed that after accessing the nonexistent key of map_xxx["nonexistent_key"], the value of the second argument will always be false ?
ps. if no, any ideas how to have this behavior?
Yes. The value to be inserted is guaranteed to be
false.In C++98, the mechanism was called default initialization, specified as zero initialization for non-classes; that’s
falsefor Booleans.Since C++03, the mechanism is called value initialization, still specified as zero initialization for non-classes; and thus still
falsefor Booleans. For example, let’s see what C++14 has to say on this.From §23.4.4.3; just substitute
boolfor “T”.From §8.5, digest the paragraphs from the bottom up:
From §4.12: