Re: hash_map
AlbertSSj wrote:
BTW, you need specialized hask<std::string> to
work with g++, IIRC.
A related question...
To specialize hash std::string this works...
namespace __gnu_cxx {
template<> struct hash<std::string>
{
size_t operator()(const std::string& __s) const {
return __stl_hash_string(__s.c_str());
}
};
}
And it actually uses the hash for const char * in stl_hash_fun.h
My question is...
It is ok in to write such code?
Absolutely. A program is free to specialize its own class templates
with standard library types. And it makes a lot of sense to do so
because the standard library types are, well, standard.
It compiles and works... But everywere is said to not add something
in the standard library (for a lot of good reasons).
gcc's hash class template is not declared within the std namespace, so
thre is nothing being added to the Standard Library.
It is better to use something like this?
struct str_hash {
size_t operator()(const std::string& __s) const {
return ::__gnu_cxx::__stl_hash_string(__s.c_str());
}
};
This str_hash function object is simply not a practical alternative for
the hash<std::string> specialization. For one, how does a program
associate a std::string type with a hash function object named
"str_hash"? And the same goes for other type specializations: would the
name of a long specialzation be a "long_hash"?
The only practical way to tie a type to an arbitrary class is with a
class template specialization - which is exactly what hash<std::string>
was in the first place.
Or maybe __stl_hash_string should not be called at all?
Why not? What is that routine good for?
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]