1
0
forked from 0ad/0ad

Improve ARRAY_SIZE.

This was SVN commit r16499.
This commit is contained in:
leper 2015-04-05 16:50:26 +00:00
parent a6a2914882
commit 7afaacda19

View File

@ -314,15 +314,15 @@ switch(x % 2)
// number of array elements
//
// (function taking a reference to an array and returning a pointer to
// an array of characters. it's only declared and never defined; we just
// need it to determine n, the size of the array that was passed.)
template<typename T, size_t n> char (*ArraySizeDeducer(T (&)[n]))[n];
// (although requiring C++, this method is much better than the standard
// sizeof(name) / sizeof(name[0]) because it doesn't compile when a
// pointer is passed, which can easily happen under maintenance.)
#define ARRAY_SIZE(name) (sizeof(*ArraySizeDeducer(name)))
template<typename T, size_t n>
constexpr size_t ArraySize(T (&)[n]) noexcept
{
return n;
}
#define ARRAY_SIZE(name) ArraySize(name)
// C99-style __func__