The QMap class already has the functionality to return a default value if an item isn't found at the specified key. This is found in
const T QMap::value ( const Key & key, const T & defaultValue ) const":http://doc.qt.nokia.com/4.7/qmap.html#value-2
This functionality is very useful, but I often find with keyed collections like a map, that I would like to insert an item with some default value at a given key. I usually implement some sort of getDefault() function. This could be defined as such:
/** Gets a value associated with a key from the QMap.
- If no entry matching the key is found, the default value is inserted and returned
- @param key - The key with which to look up the item
- @param defaultValue - The default value to insert if the associated key is not found in the map
- @return A reference to the value associated with the given key.
If there was no value at the key, this will be a reference to the newly added default value
const T& QMap::getDefault( const Key& key, const T& defaultValue );
I have added this as a suggestion in the bugtracker, but I was interested to know what other developers think of this sort of functionality. What sort of problems could this introduce, and why don't we see this sort of functionality added with many implementations of keyed containers in Qt and other libraries?