diff --git a/include/gsl/pointers b/include/gsl/pointers index c6a5ae8..436f09a 100644 --- a/include/gsl/pointers +++ b/include/gsl/pointers @@ -235,7 +235,7 @@ not_null operator+(std::ptrdiff_t, const not_null&) = delete; template >::value> struct not_null_hash { - std::size_t operator()(const T& value) const { return std::hash{}(value.get()); } + std::size_t operator()(const T& value) const noexcept { return std::hash{}(value.get()); } }; template diff --git a/tests/pointers_tests.cpp b/tests/pointers_tests.cpp index 6e72bf7..fcee2ed 100644 --- a/tests/pointers_tests.cpp +++ b/tests/pointers_tests.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -98,4 +99,19 @@ TEST(pointers_test, member_types) "check member type: element_type"); } +TEST(pointers_test, hash_noexcept_compiles) +{ + { + using Key = gsl::not_null>; + static_assert(noexcept(std::hash{}(std::declval())), + "gsl::not_null hash operator must be noexcept"); + } + + { + using Key = gsl::strict_not_null>; + static_assert(noexcept(std::hash{}(std::declval())), + "gsl::strict_not_null hash operator must be noexcept"); + } +} + } // namespace