mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8249945: Improve ARRAY_SIZE()
Make ARRAY_SIZE type-safe. Reviewed-by: tschatzl, lfoltan, dholmes
This commit is contained in:
parent
26680f0c1a
commit
8b005fa74e
2 changed files with 32 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue