Browse Source

type traits: add is_std_hashable[_v] (see #629)

Michele Caini 5 years ago
parent
commit
772c2dba2f
2 changed files with 33 additions and 4 deletions
  1. 28 4
      src/entt/core/type_traits.hpp
  2. 5 0
      test/entt/core/type_traits.cpp

+ 28 - 4
src/entt/core/type_traits.hpp

@@ -18,10 +18,10 @@ namespace entt {
  */
  */
 template<std::size_t N>
 template<std::size_t N>
 struct choice_t
 struct choice_t
-        // Unfortunately, doxygen cannot parse such a construct.
-        /*! @cond TURN_OFF_DOXYGEN */
-        : choice_t<N-1>
-        /*! @endcond */
+    // Unfortunately, doxygen cannot parse such a construct.
+    /*! @cond TURN_OFF_DOXYGEN */
+    : choice_t<N-1>
+    /*! @endcond */
 {};
 {};
 
 
 
 
@@ -562,6 +562,30 @@ template<typename Type>
 inline constexpr auto is_complete_v = is_complete<Type>::value;
 inline constexpr auto is_complete_v = is_complete<Type>::value;
 
 
 
 
+/**
+ * @brief Provides the member constant `value` to true if a given type is
+ * hashable, false otherwise.
+ * @tparam Type Potentially hashable type.
+ */
+template <typename Type, typename = void>
+struct is_std_hashable: std::false_type {};
+
+
+/*! @copydoc is_std_hashable */
+template <typename Type>
+struct is_std_hashable<Type, std::enable_if_t<std::is_convertible_v<decltype(std::declval<std::hash<Type>>()(std::declval<Type>())), std::size_t>>>
+    : std::true_type
+{};
+
+
+/**
+ * @brief Helper variable template.
+ * @tparam Type Potentially hashable type.
+ */
+template <typename Type>
+inline constexpr auto is_std_hashable_v = is_std_hashable<Type>::value;
+
+
 /**
 /**
  * @brief Provides the member constant `value` to true if a given type is empty
  * @brief Provides the member constant `value` to true if a given type is empty
  * and the empty type optimization is enabled, false otherwise.
  * and the empty type optimization is enabled, false otherwise.

+ 5 - 0
test/entt/core/type_traits.cpp

@@ -119,6 +119,11 @@ TEST(TypeTraits, IsComplete) {
     static_assert(!entt::is_complete_v<void>);
     static_assert(!entt::is_complete_v<void>);
 }
 }
 
 
+TEST(TypeTraits, IsStdHashable) {
+    static_assert(entt::is_std_hashable_v<int>);
+    static_assert(!entt::is_std_hashable_v<not_comparable>);
+}
+
 TEST(TypeTraits, ConstnessAs) {
 TEST(TypeTraits, ConstnessAs) {
     static_assert(std::is_same_v<entt::constness_as_t<int, char>, int>);
     static_assert(std::is_same_v<entt::constness_as_t<int, char>, int>);
     static_assert(std::is_same_v<entt::constness_as_t<const int, char>, int>);
     static_assert(std::is_same_v<entt::constness_as_t<const int, char>, int>);