wlog

[index] [prog]

viewing // 1


:: BUILD_BUG_ON_ZERO and static assertions in C

@ 2017-03-24 in /prog/

Here is an interesting macro used in the Linux kernel:

/* Force a compilation error if condition is true, but also produce a
   result (of value 0 and type size_t), so the expression can be used
   e.g. in a structure initializer (or where-ever else comma expressions
   aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))

Such perversion is actually pretty common in C, and the kernel has plenty more of that to choose from. The genesis of this macro seems to be the (until recently) lack of static assertions in the standard, but even now it's not totally obsolete. So what follows are some notes about that.

Read more...


[site][wlog][all] ♦ [1/1]