8249945: Improve ARRAY_SIZE()

Make ARRAY_SIZE type-safe.

Reviewed-by: tschatzl, lfoltan, dholmes
This commit is contained in:
Kim Barrett 2020-07-24 05:07:37 -04:00
parent 26680f0c1a
commit 8b005fa74e
2 changed files with 32 additions and 1 deletions

View file

@ -1095,7 +1095,11 @@ template<class T> static void swap(T& a, T& b) {
b = tmp;
}
#define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
// array_size_impl is a function that takes a reference to T[N] and
// returns a reference to char[N]. It is not ODR-used, so not defined.
template<typename T, size_t N> char (&array_size_impl(T (&)[N]))[N];
#define ARRAY_SIZE(array) sizeof(array_size_impl(array))
//----------------------------------------------------------------------------------------------------
// Sum and product which can never overflow: they wrap, just like the