Subtle MT problem with __gnu_cxx::hash_map

Joe Buck Joe.Buck@synopsys.COM
Tue Nov 23 18:29:00 GMT 2004


On Tue, Nov 23, 2004 at 10:02:19AM -0500, Paul Dubuc wrote:
> No, but operator[] always does a read, whether it inserts or not. So this is not 
> a relevant comparison.  Can't you see the ambiguity here?  There is no analogy 
> to operator[] because it is a find() which will insert and return an empty 
> element is one is not already there.

If the function with the interface that you want is not present, then
just write it:

template<typename Container, typename KeyType, typename ResultType>
ResultType lookupKey(const Container& container, const KeyType& key,
		     const ResultType& defaultValue)
{
  Container::const_iterator iter = container.find(key);
  return iter == container.end() ? defaultValue : iter->second;
}

The above should be good for hash_map or map.



More information about the Libstdc++ mailing list