Skip to content

Commit 6d4b93f

Browse files
ldionneNoumanAmir657
authored andcommitted
[libc++][NFC] Add a static assertion to document an assumption in std::hash (llvm#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 69e6179 commit 6d4b93f

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
@@ -407,7 +407,11 @@ struct _LIBCPP_TEMPLATE_VIS hash<long> : public __unary_function<long, size_t> {
407407

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

413417
template <>

0 commit comments

Comments
 (0)