Is there a static_assert replacement which satisfies the C99 standard?

前端 未结 2 1855
刺人心
刺人心 2021-02-13 18:19

I have been trying to implement a method similar to static_assert which is defined in the C++11 standard. The main problem is how does the C++ compiler write the te

2条回答
  •  囚心锁ツ
    2021-02-13 19:06

    Not sure i understand question, but C11 have _Static_assert(condition, errmessage). In C99 this functionality was missing but, depending on compiler, it could be possible to emulate. E.g. for gcc (unfortulately clang doesn't support attribute(error))

    #define MY_STATIC_ASSERT(cnd, descr) ({ \
        extern int __attribute__ ((error("static assert failed: (" #cnd ") (" #descr ")"))) \
                   compile_time_check(void); \
        ((cnd) ? 0 : compile_time_check()), 0; \
    })
    

提交回复
热议问题