mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-09-29 21:05:40 +00:00
better ARRAY_SIZE macro that refuses to compile when passed a pointer.
this prevents bugs along the lines of the recently reported StringStripper issue. This was SVN commit r5381.
This commit is contained in:
+13
-2
@@ -57,8 +57,19 @@ const size_t MiB = 1ul << 20;
|
||||
const size_t GiB = 1ul << 30;
|
||||
|
||||
|
||||
/// number of array elements
|
||||
#define ARRAY_SIZE(name) (sizeof(name) / sizeof(name[0]))
|
||||
//
|
||||
// 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)))
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user