Static assert in C

前端 未结 12 717
感情败类
感情败类 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:59

    This worked for some old gcc. Sorry that I forgot what version it was:

    #define _cat(x, y) x##y
    
    #define _sassert(exp, ln)\
    extern char _cat(SASSERT_, ln)[1]; \
    extern char _cat(SASSERT_, ln)[exp ? 1 : 2]
    
    #define sassert(exp) _sassert((exp), __LINE__)
    
    //
    sassert(1 == 2);
    
    //
    #148 declaration is incompatible with "char SASSERT_134[1]" (declared at line 134)  main.c  /test/source/controller line 134    C/C++ Problem
    

提交回复
热议问题