BOOST_STATIC_ASSERT without boost

One other trick (which can be used in C) is to try to build an array with a negative size if the assert fail:

#define ASSERT(cond) int foo[(cond) ? 1 : -1]

as a bonus, you may use a typedef instead of an object, so that it is usable in more contexts and doesn’t takes place when it succeed:

#define ASSERT(cond) typedef int foo[(cond) ? 1 : -1]

finally, build a name with less chance of name clash (and reusable at least in different lines):

#define CAT_(a, b) a ## b
#define CAT(a, b) CAT_(a, b)
#define ASSERT(cond) typedef int CAT(AsSeRt, __LINE__)[(cond) ? 1 : -1]

Leave a Comment