hash_map<> and string...

Gabriel Dos Reis gdr@codesourcery.com
Tue Nov 21 13:36:00 GMT 2000


Benjamin Kosnik <bkoz@redhat.com> writes:

| Sorry for the delay, I'm going back through this newsgroup for things
| I apparently forgot on the first pass.
| 
| ------
| 
| The stl_hash_fun.h in CVS has:
| 
| inline size_t __stl_hash_string(const char* __s)
| {
|   unsigned long __h = 0; 
|   for ( ; *__s; ++__s)
|     __h = 5*__h + *__s;
|   
|   return size_t(__h);
| }
| 
| __STL_TEMPLATE_NULL struct hash<char*>
| {
|   size_t operator()(const char* __s) const { return __stl_hash_string(__s); }
| };
| 
| __STL_TEMPLATE_NULL struct hash<const char*>
| {
|   size_t operator()(const char* __s) const { return __stl_hash_string(__s); }
| };

His point was that the class template hash<> should also be specialized
for std::string since it is the C++ politically correct way of spelling
"character string".

| I suspect this will get you where you want to go. If not, you're
| pretty much out of luck, as only fundamental types are going in this
| file, apparently.

Well, any reasonable hash table library should come with a hash
function for fundmantal types or nearby types (i.e. pointer types).
It is also understandable to require the library to come with hash
functions defined for any type it happens to define or use, for
completeness purpose.

| Is it really that much extra work to define this yourself?

It is not really hard to add support for hash<std::string> but that has
a low priority on my TODO list.  And if we start providing support
for std::string, there is no reason we should stop there... 

-- Gaby
CodeSourcery, LLC                       http://www.codesourcery.com


More information about the Libstdc++ mailing list