编译时断言

来源:互联网 发布:新型网络诈骗手段新闻 编辑:程序博客网 时间:2024/05/16 01:27

运行时断言大家都用过,但如果想当某个条件不满足时在编译时就让程序编译不通过,即编译时断言(不能产生副作用)是什么样子的呢。可参考如下:


1, 参考linux内核静态断言

/* Force a compilation error if condition is true */


#ifdef WIN32
    #define BUILD_BUG_ON(e)  {typedef char __static_assert_t[!(e)];}
#else
    #define BUILD_BUG_ON(e) ((void)sizeof(char[1 - 2*!!(e)]))
#endif


2, boost库也提供了静态断言,C++代码可直接使用

#include <boost/static_assert.hpp> 

BOOST_STATIC_ASSERT(sizeof(DiskChunkInfo_t) == 4400);


1 1