Skip to content

Commit

Permalink
hashing for std::optional
Browse files Browse the repository at this point in the history
otherwise only possible if optional::value_type
has std::hash support
  • Loading branch information
felixguendling committed Oct 10, 2024
1 parent f135831 commit 777618a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/cista/hashing.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ struct has_std_hash<
T, std::void_t<decltype(std::declval<std::hash<T>>()(std::declval<T>()))>>
: std::true_type {};

template <typename T, typename = void>
struct is_optional : std::false_type {};

template <typename T>
struct is_optional<std::optional<T>> : std::true_type {};

} // namespace detail

template <typename T>
Expand Down Expand Up @@ -116,6 +122,8 @@ struct hashing {
return h;
} else if constexpr (is_strong_v<Type>) {
return hashing<typename Type::value_t>{}(el.v_, seed);
} else if constexpr (detail::is_optional<Type>::value) {
return el.has_value() ? 0U : hashing<typename Type::value_type>{}(*el);
} else {
static_assert(has_hash_v<Type> || std::is_scalar_v<Type> ||
has_std_hash_v<Type> || is_iterable_v<Type> ||
Expand Down

0 comments on commit 777618a

Please sign in to comment.