I really wish there was an API "Hall of Shame" with attached discussion minutes showing the exact points where a group of otherwise sane people decided to kludge these things in.
I really wish there was an API "Hall of Shame" with attached discussion minutes showing the exact points where a group of otherwise sane people decided to kludge these things in.
It's better than the alternatives though, I would be interested to hear how you handle this in your version. Leaving the behaviour undefined for non-existent keys is likely to cause far worse bugs, throwing an exception would be inconsistent with the rest of the stl.
The could have left it out altogether, but would mean losing some nice properties - operator[] returning a reference makes it possible to assign into the map directly ( a[3] = 5; ). Also since the value is default initialized, you can write something like a counter easily, much like a python defaultdict:
for (auto id in ids) {
a[id] += 10;
}
You can always stick to .find and .insert if you prefer the more explicit behaviour.Though not consistent with how STL works, it is consistent with how our containers work and how we use maps. YMMV.
Also, since we wrote it, we're free to change the behavior if a better way manifests itself. So, if you have any suggestions, let fly.
(I don't know if that would be better, since it's even more magic and it still isn't perfect.)
Now, off to pour Courvoisier on those funny bumps in my groin...