无警告C语言编译断言

来源:互联网 发布:西安网络推广咨询电 编辑:程序博客网 时间:2024/05/18 22:18

方法一:

#ifndef STATIC_ASSERT#define STATIC_ASSERT(exp) ((void)sizeof(struct{int:-!(exp);))#endif

方法二:

#ifndef STATIC_ASSERT#define STATIC_ASSERT(exp) ((void)sizeof(char[exp?1:-1]))#endif

方法三:

#ifndef STATIC_ASSERT#define STATIC_ASSERT(exp) switch(0) { case 0: case (exp):; }#endif


注意:condition必须是常量表达式,编译期可计算的。

0 0