r/cpp • u/Krystian-Piekos • Apr 08 '26
Hashing in C++26
https://blog.infotraining.pl/hashing-in-cpp-26How to implement hash for custom classes in C++26.
80
Upvotes
r/cpp • u/Krystian-Piekos • Apr 08 '26
How to implement hash for custom classes in C++26.
View all comments
Show parent comment
1
u/Krystian-Piekos Apr 09 '26 edited Apr 09 '26
Thanks for the insightful comment. The opt‑in mechanism I originally chose doesn’t work well for two scenarios. I could switch to a traits‑based approach:
Opt‑in would then look like:
This lets us conditionally enable hashing:
The syntax works, but it’s a bit awkward.
I wondered whether annotations could help:
But using annotations for conditional opt‑in is tricky. Maybe something like this could be acceptable: