Hacker News new | ask | show | jobs
by rwmj 1514 days ago
This is what qemu uses:

  #define QEMU_BUILD_BUG_ON(x) \
    typedef char qemu_build_bug_on[(x)?-1:1] __attribute__((unused));
So you can write stuff like:

  QEMU_BUILD_BUG_ON(sizeof (struct foo) == 128);
(for example if the struct is used for some network protocol and so it must be 128 bytes long).
1 comments

We've switched to _Static_assert() now we can assume all our compilers support it:

   #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)

   #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)