diff -r 3155f2c05f89 -r 47a2f9d10ebe src/macros.h --- a/src/macros.h Tue Apr 08 10:54:32 2014 +0300 +++ b/src/macros.h Tue Apr 08 11:00:29 2014 +0300 @@ -17,12 +17,25 @@ */ #pragma once +#include #ifndef __GNUC__ # define __attribute__(X) #endif -#define countof(A) ((int) (sizeof A / sizeof *A)) +// ============================================================================= +// +#define countof(A) (safeCountOf::value, (sizeof A / sizeof *A)>()) + +template +inline constexpr int safeCountOf() __attribute__ ((always_inline)); + +template +inline constexpr int safeCountOf() +{ + static_assert (isArray, "parameter to countof must be an array"); + return elems; +} // ============================================================================= //