I am developing a (C ++) library that uses unordered containers. They require hashes (usually the specialized structure of the std::hash template) for the types of elements they store. In my case, these elements are classes that encapsulate string literals, similar to the conststr example at the bottom of this page . STL offers specialization for char constant pointers, which, however, only compute pointers, as described here in the Remarks section.
There is no specialization for strings C. std::hash<const char*> creates a hash of the pointer value (memory address), it does not check the contents of any array of characters.
Although it is very fast (or so I think), it is not guaranteed by the C ++ standard whether several identical string literals are stored at the same address, as described in this question . If this is not the case, the first hash condition will fail:
For two parameters k1 and k2 equal, std::hash<Key>()(k1) == std::hash<Key>()(k2)
I would like to selectively calculate the hash using the provided specialization, if the above guarantee is given, or some other algorithm otherwise. Although calling on those who include my headers, or building my library to define a specific macro, an implementation may be preferable.
Is there any macro in any C ++ implementation, but mostly g ++ and clang, whose definition ensures that multiple identical string literals are stored at the same address?
Example:
#ifdef __GXX_SAME_STRING_LITERALS_SAME_ADDRESS__ const char str1[] = "abc"; const char str2[] = "abc"; assert( str1 == str2 ); #endif
c ++ string-literals c ++ 14
Kalrish
source share