I implemented std::hash<std::string> in the usual way, by specializing the std::hash template. But then I realized that these should already be provided by the <string> header (https://www.cppreference.com/w/cpp/string/basic_string/hash.html). Unexpectedly, the program compiled (using both the latest MSVC and GCC-15.1) and it worked using my implementation.
Was I supposed to get a 'template already specialized' error?
namespace std
{
template <>
struct hash<std::string>
{
static auto operator()(const std::string&) noexcept -> std::size_t
{
return 0uz;
}
};
} // namespace std
namespace std(except for specific cases like specialization for your custom types) invokes UB.