Why doesn't the C++ standard specify that
std::hash<T>
is specialized forchar*
,const char*
,unsigned char*
,const unsigned char*
, etc?
It looks like it originated from proposal N1456. (emphasis mine)
Some earlier hash table implementations gave char* special treatment: it specialized the default hash function to look at character array being pointed to, rather than the pointer itself. This proposal removes that special treatment. Special treatment makes it slightly easier to use hash tables for C string, but at the cost of removing uniformity and making it harder to write generic code. Since naive users would generally be expected to use std::basic_string instead of C strings, the cost of special treatment outweighs the benefit.
If I'm interpreting this correctly, the reasoning is that supporting C style strings would break code that generically acts on hashes of pointers.
Any harm in injecting my own specializations into the std namespace for my own code?
There is potential harm, yes.
- In the future, anything you added to the
std
namespace could collide with a new symbol name. - In the present, anything you add to the
std
namespace could be a "better match" for other components of the standard library, silently breaking behavior.