用 _Static_assert 做编译期检查

版本新特性 / C11

// *** before: ***
//C99 had no check at compile time; the trick was an array whose size
//turns negative when the condition fails, with a baffling message
#define STATIC_CHECK(cond, name) typedef char name[(cond) ? 1 : -1]

STATIC_CHECK(sizeof(int) >= 4, intIsWideEnough);
//a false condition gave: size of array 'intIsWideEnough' is negative

// *** in version C11: ***
//the check and its message are part of the language
_Static_assert(sizeof(int) >= 4, "int must hold 32 bits");
//_Static_assert(sizeof(int) == 2, "16-bit int");
//    <- error: static assertion failed: "16-bit int"

//<assert.h> spells it static_assert; C23 made that a keyword
//and the message optional
printf("sizeof(int) is %zu\n", sizeof(int));