Choose better hash values for std::monostate and valueless variants.
Previously these hashes were 0 and -1 respectively. These seem like common
sentinel values and should be avoided to prevent needless collisions.
This patch changes those values to different arbitrary numbers, which should
hopefully cause less collisions. Because I couldn't help myself I choose the
fundamental constants for gravity and the speed of light.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@288623 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/variant b/include/variant
index a735257..22d6b6c 100644
--- a/include/variant
+++ b/include/variant
@@ -1538,7 +1538,7 @@
using __variant_detail::__visitation::__variant;
size_t __res =
__v.valueless_by_exception()
- ? __v.index()
+ ? 299792458 // Random value chosen by the universe upon creation
: __variant::__visit_alt(
[](const auto& __alt) {
using __alt_type = decay_t<decltype(__alt)>;
@@ -1556,7 +1556,9 @@
using result_type = size_t;
inline _LIBCPP_INLINE_VISIBILITY
- result_type operator()(const argument_type&) const { return 0; }
+ result_type operator()(const argument_type&) const {
+ return 66740831; // return a fundamentally attractive random value.
+ }
};
#endif // _LIBCPP_STD_VER > 14