Re: cstring hash
In article <20130728191552.6d7fe783@bother.homenet>,
chris@cvine--nospam--.freeserve.co.uk (Chris Vine) wrote:
Ah yes, I understand you now. You want to hash on the contents of
a C string, not its address, in a similar way to std::string.
This could only be of any significant value with allocated strings
rather than literals. I guess those who wrote the standard took
the view that no sane person would hold allocated strings by
pointer in an unordered associative container (apart from anything
else, it would not be exception safe), and that std::string was
there for that purpose.
A curious assumption. It could be exception-safe if something else
was responsible for deleting the strings. For example, a string class
other than std::string, or a C-style interface, or some low-level
character code.
Having a special separate hash class for C strings probably
seemed like a bizarre addition to the standards committee (so
far as it was thought of at all), but you could always put the
idea forward and see what happens.
Boost has hash_combine, and I gather this was proposed for the
standard library but didn't make it in. If it had, then something
like:
size_t hash( const char *str ) {
size_t h = 0;
while (*str)
hash_combine( h, *str++ );
return h;
}
would have done.
-- Dave Harris, Nottingham, UK.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]