this post was submitted on 24 Dec 2023
18 points (84.6% liked)
C++
1755 readers
1 users here now
The center for all discussion and news regarding C++.
Rules
- Respect instance rules.
- Don't be a jerk.
- Please keep all posts related to C++.
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
That's a "you're using it wrong" problem. The
operator[]
is designed to "Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist. "The "0 value" just so happens to be the result you get from a default initializer whose default initialization corresponds to zero-initialization.
If you want to use a
std::map
to access the element associated with a key, you need to either useat
and handle an exception if no such key exists, or usefind
.My point is that the design is wrong. Nobody expects [] as lvalue to update a value. Your argument is descriptive, mine is prespcriptive. I am saying that the C++ committee is wrong on this one (or whoever designed it this way)