Skip to content

Commit fcf3fe3

Browse files
maflckoMarcoFalke
andauthored
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>
1 parent 9f9f65c commit fcf3fe3

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

include/gsl/pointers

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
235235
template <class T, class U = typename T::element_type, bool = std::is_default_constructible<std::hash<U>>::value>
236236
struct not_null_hash
237237
{
238-
std::size_t operator()(const T& value) const { return std::hash<U>{}(value.get()); }
238+
std::size_t operator()(const T& value) const noexcept { return std::hash<U>{}(value.get()); }
239239
};
240240

241241
template <class T, class U>

tests/pointers_tests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <gsl/pointers>
44

5+
#include <functional>
56
#include <memory>
67
#include <type_traits>
78
#include <utility>
@@ -98,4 +99,19 @@ TEST(pointers_test, member_types)
9899
"check member type: element_type");
99100
}
100101

102+
TEST(pointers_test, hash_noexcept_compiles)
103+
{
104+
{
105+
using Key = gsl::not_null<std::shared_ptr<int>>;
106+
static_assert(noexcept(std::hash<Key>{}(std::declval<Key>())),
107+
"gsl::not_null hash operator must be noexcept");
108+
}
109+
110+
{
111+
using Key = gsl::strict_not_null<std::shared_ptr<int>>;
112+
static_assert(noexcept(std::hash<Key>{}(std::declval<Key>())),
113+
"gsl::strict_not_null hash operator must be noexcept");
114+
}
115+
}
116+
101117
} // namespace

0 commit comments

Comments
 (0)