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

This commit is contained in:
Michele Caini
2021-05-08 10:37:36 +02:00
parent c4b169edd1
commit c40f0ef2bb
2 changed files with 11 additions and 1 deletions

View File

@@ -442,7 +442,11 @@ template<typename Type>
template<typename Type>
[[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
-> 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>);
}
}

View File

@@ -11,6 +11,11 @@ struct not_comparable {
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) {
static_assert(entt::size_of_v<void> == 0u);
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, std::unordered_map<int, not_comparable>>>);
static_assert(entt::is_equality_comparable_v<nlohmann_json_like>);
static_assert(!entt::is_equality_comparable_v<void>);
}