Actually, I think there's a limitation: suppose you have a std::map<std::string, std::size_t>, as per my word count example. If you try a has_key(map, "word"), the code will not compile since the string literal's type does not match the map's key type.
One solution is to change the call to has_key(map, std::string("word")). A possibly better solution is to change the function template to:
Code:
template<typename T>
bool has_key(const T& map, const typename T::key_type& key)
{
return map.find(key) != map.end();
}
EDIT:
I'm dead against this kind of encapsulation. has_key is supposed to be a member function of std::map instead, not broken into bits outside.
This is the same thing as discussed in the "std::string is a monolithic type" article by Sutter. has_key() can be implemented as a non-member non-friend.