# HG changeset patch # User Santeri Piippo # Date 1396944029 -10800 # Node ID 47a2f9d10ebedceab129d1cdf9c852c22acd13cf # Parent 3155f2c05f89ae15effe8b6c5307996e93eb5b53 - added safety check to the countof macro 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; +} // ============================================================================= //