Static assert in C

前端 未结 12 753
感情败类
感情败类 2020-11-22 16:22

What\'s the best way to achieve compile time static asserts in C (not C++), with particular emphasis on GCC?

12条回答
  •  半阙折子戏
    2020-11-22 16:57

    C11 standard adds the _Static_assert keyword.

    This is implemented since gcc-4.6:

    _Static_assert (0, "assert1"); /* { dg-error "static assertion failed: \"assert1\"" } */
    

    The first slot needs to be an integral constant expression. The second slot is a constant string literal which can be long (_Static_assert(0, L"assertion of doom!")).

    I should note that this is also implemented in recent versions of clang.

提交回复
热议问题