Index: include/core/SkTypes.h |
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h |
index a1719f89e10e4f86a256c6aa178e94e8651e3c9e..01736566457a07fa7c9b2e1a68baeff874f922db 100644 |
--- a/include/core/SkTypes.h |
+++ b/include/core/SkTypes.h |
@@ -296,9 +296,28 @@ static inline bool SkIsU16(long x) { |
#define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1) |
#endif |
+// The SK_ARRAY_COUNT(arr) macro returns the # of elements in an array arr. |
+// The expression is a compile-time constant, and therefore can be |
+// used in defining new arrays, for example. If you use SK_ARRAY_COUNT on |
+// a pointer by mistake, you will get a compile-time error. |
+// |
+// This template function declaration is used in defining SK_ARRAY_COUNT. |
+// Note that the function doesn't need an implementation, as we only |
+// use its type. |
+template <typename T, size_t N> |
+char (&ArraySizeHelper(T (&array)[N]))[N]; |
+ |
+// That gcc wants both of these prototypes seems mysterious. VC, for |
+// its part, can't decide which to use (another mystery). Matching of |
+// template overloads: the final frontier. |
+#ifndef _MSC_VER |
+template <typename T, size_t N> |
+char (&ArraySizeHelper(const T (&array)[N]))[N]; |
+#endif |
+ |
/** Returns the number of entries in an array (not a pointer) |
*/ |
-#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) |
+#define SK_ARRAY_COUNT(array) (sizeof(::ArraySizeHelper(array))) |
#define SkAlign2(x) (((x) + 1) >> 1 << 1) |
#define SkIsAlign2(x) (0 == ((x) & 1)) |