From bb546e19885ae084823e0315e93564a44c0a8257 Mon Sep 17 00:00:00 2001 From: Gil Date: Fri, 1 Jun 2018 13:42:47 -0700 Subject: Fix Firestore compilation under Xcode < 9.2 (#1367) * Don't rely on specialization failure to determine when std::hash is unavailable. Instead manually declare the conditions under which std::hash should be defined. * Fix detection of Objective-C classes in Xcode < 9.2 std::is_base_of{} is false there so the overloads defined for Objective-C types weren't getting enabled. * Add explicit tests for StringFormat using Objective-C objects * Add explicit tests for HasStdHash --- .../core/src/firebase/firestore/util/hashing.h | 39 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'Firestore/core/src/firebase/firestore/util/hashing.h') diff --git a/Firestore/core/src/firebase/firestore/util/hashing.h b/Firestore/core/src/firebase/firestore/util/hashing.h index d8058c8..21c0bd6 100644 --- a/Firestore/core/src/firebase/firestore/util/hashing.h +++ b/Firestore/core/src/firebase/firestore/util/hashing.h @@ -18,6 +18,7 @@ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_HASHING_H_ #include +#include #include namespace firebase { @@ -48,6 +49,41 @@ namespace util { namespace impl { +/** + * A type trait that identifies whether or not std::hash is available for a + * given type. + * + * This type should not be necessary since specialization failure on an + * expression like `decltype(std::hash{}(value)` should be enough to disable + * overloads that require `std::hash` to be defined but unfortunately some + * standard libraries ship with std::hash defined for all types that only + * fail later (e.g. via static_assert). One such implementation is the libc++ + * that ships with Xcode 8.3.3, which is a supported platform. + */ +template +struct has_std_hash { + // There may be other types for which std::hash is defined but they don't + // matter for our purposes. + enum { + value = std::is_arithmetic{} || std::is_pointer{} || + std::is_same{} + }; + + constexpr operator bool() const { + return value; + } +}; + +/** + * A type that's equivalent to size_t if std::hash is defined or a compile + * error otherwise. + * + * This is effectively just a safe implementation of + * `decltype(std::hash{}(std::declval()))`. + */ +template +using std_hash_type = typename std::enable_if{}, size_t>::type; + /** * Combines a hash_value with whatever accumulated state there is so far. */ @@ -100,8 +136,7 @@ auto RankedInvokeHash(const K& value, HashChoice<0>) -> decltype(value.Hash()) { * @return The result of `std::hash{}(value)` */ template -auto RankedInvokeHash(const K& value, HashChoice<1>) - -> decltype(std::hash{}(value)) { +std_hash_type RankedInvokeHash(const K& value, HashChoice<1>) { return std::hash{}(value); } -- cgit v1.2.3