...
According to the C++ 0X draft standard, the constant-expression
in a static assert declaration is a constant expression that can be converted to bool
at compile time. If the value of the converted expression is true, the declaration has no effect. Otherwise the program is ill-formed, and a diagnostic message (which includes the text of the string-literal
) is issued at compile time. For example:
Code Block |
---|
/* Passes */ static_assert( sizeof(int) <= sizeof(void*), "sizeof(int) <= sizeof(void*)" ); /* PassesFails */ static_assert( sizeof(double) <= sizeof(int), "sizeof(double) <= sizeof(int)" ); /* Fails */ |
Static assertion is not available in C99, but the facility is being considered for inclusion in C1X by the ISO/IEC WG14 international standardization working group.
...
Code Block | ||
---|---|---|
| ||
#define JOIN(x, y) JOIN_AGAIN(x, y)
#define JOIN_AGAIN(x, y) x ## y
#define static_assert(e) \
typedef char JOIN(assertion_failed_at_line_, _LINE_) \
[(e) ? 1 : -1]
struct timer {
uint8_t MODE;
uint32_t DATA;
uint32_t COUNT;
};
static_assert(offsetof(struct timer, DATA) == 4);
|
...