gsl::not_null: C.89: Make a hash noexcept (#1236)

Without this change, the guideline
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c89-make-a-hash-noexcept
would be violated.

The test fails before the changes here:

```
tests/pointers_tests.cpp:102:23: error: static assertion failed due to requirement 'noexcept(std::hash<gsl::not_null<std::shared_ptr<int>>>{}(std::declval()))': gsl::not_null hash operator must be noexcept
  102 |         static_assert(noexcept(std::hash<Key>{}(std::declval<Key>())),
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tests/pointers_tests.cpp:108:23: error: static assertion failed due to requirement 'noexcept(std::hash<gsl::strict_not_null<std::shared_ptr<int>>>{}(std::declval()))': gsl::strict_not_null hash operator must be noexcept
  108 |         static_assert(noexcept(std::hash<Key>{}(std::declval<Key>())),
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
This commit is contained in:
maflcko
2026-03-23 12:18:30 -06:00
committed by GitHub
co-authored by MarcoFalke
parent 9f9f65c7e2
commit fcf3fe37c6
2 changed files with 17 additions and 1 deletions
+1 -1
View File
@@ -235,7 +235,7 @@ not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
template <class T, class U = typename T::element_type, bool = std::is_default_constructible<std::hash<U>>::value>
struct not_null_hash
{
std::size_t operator()(const T& value) const { return std::hash<U>{}(value.get()); }
std::size_t operator()(const T& value) const noexcept { return std::hash<U>{}(value.get()); }
};
template <class T, class U>