Bläddra i källkod

type traits: is_equality_comparable support for nlohmann json like types (where T::value_type is T) - close #701

Michele Caini 4 år sedan
förälder
incheckning
c40f0ef2bb
2 ändrade filer med 11 tillägg och 1 borttagningar
  1. 5 1
      src/entt/core/type_traits.hpp
  2. 6 0
      test/entt/core/type_traits.cpp

+ 5 - 1
src/entt/core/type_traits.hpp

@@ -442,7 +442,11 @@ template<typename Type>
 template<typename Type>
 template<typename Type>
 [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
 [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
 -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
 -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
-    return is_equality_comparable<typename Type::value_type>(choice<2>);
+    if constexpr(std::is_same_v<typename Type::value_type, Type>) {
+        return is_equality_comparable<Type>(choice<0>);
+    } else {
+        return is_equality_comparable<typename Type::value_type>(choice<2>);
+    }
 }
 }
 
 
 
 

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

@@ -11,6 +11,11 @@ struct not_comparable {
     bool operator==(const not_comparable &) const = delete;
     bool operator==(const not_comparable &) const = delete;
 };
 };
 
 
+struct nlohmann_json_like {
+    using value_type = nlohmann_json_like;
+    bool operator==(const nlohmann_json_like &) const { return true; }
+};
+
 TEST(TypeTraits, SizeOf) {
 TEST(TypeTraits, SizeOf) {
     static_assert(entt::size_of_v<void> == 0u);
     static_assert(entt::size_of_v<void> == 0u);
     static_assert(entt::size_of_v<char> == sizeof(char));
     static_assert(entt::size_of_v<char> == sizeof(char));
@@ -102,6 +107,7 @@ TEST(TypeTraits, IsEqualityComparable) {
     static_assert(!entt::is_equality_comparable_v<std::unordered_map<int, not_comparable>>);
     static_assert(!entt::is_equality_comparable_v<std::unordered_map<int, not_comparable>>);
     static_assert(!entt::is_equality_comparable_v<std::unordered_map<int, std::unordered_map<int, not_comparable>>>);
     static_assert(!entt::is_equality_comparable_v<std::unordered_map<int, std::unordered_map<int, not_comparable>>>);
 
 
+    static_assert(entt::is_equality_comparable_v<nlohmann_json_like>);
     static_assert(!entt::is_equality_comparable_v<void>);
     static_assert(!entt::is_equality_comparable_v<void>);
 }
 }