Skip to content

Commit d301b59

Browse files
authored
[libc++][NFC] Add a static assertion to document an assumption in std::hash (#114440)
The implementation of std::hash for unsigned long makes the (correct) assumption that size_t is at least as large as unsigned long. If that were not the case on a platform, the implementation of std::hash for unsigned long would be absolutely terrible. Add a static assertion to document that assumption.
1 parent 64314de commit d301b59

File tree

1 file changed

+5
-1
lines changed
  • libcxx/include/__functional

1 file changed

+5
-1
lines changed

libcxx/include/__functional/hash.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,11 @@ struct _LIBCPP_TEMPLATE_VIS hash<long> : public __unary_function<long, size_t> {
406406

407407
template <>
408408
struct _LIBCPP_TEMPLATE_VIS hash<unsigned long> : public __unary_function<unsigned long, size_t> {
409-
_LIBCPP_HIDE_FROM_ABI size_t operator()(unsigned long __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
409+
_LIBCPP_HIDE_FROM_ABI size_t operator()(unsigned long __v) const _NOEXCEPT {
410+
static_assert(sizeof(size_t) >= sizeof(unsigned long),
411+
"This would be a terrible hash function on a platform where size_t is smaller than unsigned long");
412+
return static_cast<size_t>(__v);
413+
}
410414
};
411415

412416
template <>

0 commit comments

Comments
 (0)