Ray's Knowledge Base
Unlock

libstdc++ before GCC 13 has no std::hash for a string with a custom allocator

PitfallVerified 5 Nov 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A std container keyed by a string type that uses a custom allocator (here dstd::String) builds with Clang and libc++ on macOS. The same code fails to compile on Linux with GCC 12.

Cause#

libstdc++ before GCC 13 provides std::hash for std::basic_string only when the string uses the standard allocator. libc++ hashes any allocator, so the Mac build hides the gap.

Fix#

Add a std::hash specialization for the string type. Make it return the same value as std::hash<std::string> for the same bytes, and add a test that checks this. Then the type works as a key in any std container.

Evidence#

dftracer-utils sweep step "utilities": the Corona GCC 12 job failed on a dstd::String key while the Mac build passed. After the specialization was added in include/dftracer/utils/dstd/string.h, the Corona job passed (525 of 525 tests, 0 warnings).

Also found by searching

  • std::hash of a string with a custom allocator fails on GCC 12
  • a std::unordered_map keyed by an allocator-aware string builds on macOS but not on Linux GCC 12
  • no match for call to std::hash with basic_string and a non-default allocator
  • how do I use a string with my own allocator as a std container key