File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
src/Beman/Optional26/tests Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -1147,4 +1147,21 @@ class optional<T&> {
1147
1147
1148
1148
} // namespace beman::optional26
1149
1149
1150
+ namespace std {
1151
+ template <typename T>
1152
+ requires requires (T a) {
1153
+ { std::hash<remove_const_t <T>>{}(a) } -> std::convertible_to<std::size_t >;
1154
+ }
1155
+ struct hash <beman::optional26::optional<T>> {
1156
+ static_assert (!is_reference_v<T>, " hash is not enabled for reference types" );
1157
+ size_t operator ()(const beman::optional26::optional<T>& o) const
1158
+ noexcept (noexcept (hash<remove_const_t <T>>{}(*o))) {
1159
+ if (o) {
1160
+ return std::hash<std::remove_const_t <T>>{}(*o);
1161
+ } else {
1162
+ return 0 ;
1163
+ }
1164
+ }
1165
+ };
1166
+ } // namespace std
1150
1167
#endif // BEMAN_OPTIONAL26_OPTIONAL_HPP
Original file line number Diff line number Diff line change @@ -596,6 +596,21 @@ TEST(OptionalTest, RangeTest) {
596
596
}
597
597
}
598
598
599
+ TEST (OptionalTest, HashTest) {
600
+ beman::optional26::optional<int > o1 = beman::optional26::nullopt ;
601
+ beman::optional26::optional<int > o2 = beman::optional26::nullopt ;
602
+ beman::optional26::optional<int > o3 = 42 ;
603
+ beman::optional26::optional<int > o4 = 42 ;
604
+
605
+ auto h1 = std::hash<beman::optional26::optional<int >>{}(o1);
606
+ auto h2 = std::hash<beman::optional26::optional<int >>{}(o2);
607
+ auto h3 = std::hash<beman::optional26::optional<int >>{}(o3);
608
+ auto h4 = std::hash<beman::optional26::optional<int >>{}(o4);
609
+
610
+ EXPECT_EQ (h1, h2);
611
+ EXPECT_EQ (h3, h4);
612
+ }
613
+
599
614
TEST (ViewMaybeTest, Constructors) {
600
615
std::ranges::single_view<std::optional<int >> s;
601
616
std::ranges::single_view<std::optional<int >> s2{s};
You can’t perform that action at this time.
0 commit comments