Compile Time Assertion..

The most seen assertion are during runtime, but this one is at compile time, to give the error more quickly..

 

I do not quite understand why need this kind of check.. The following is code from OVS..

#define BUILD_ASSERT(EXPR) \
  typedef char AssertOnCompileFailed[(EXPR)?1:-1]

#define BUILD_ASSERT_DECL(EXPR) BUILD_ASSERT(EXPR)
typedef struct _NlMsgErr {
  INT error;
  NL_MSG_HDR nlMsg;
} NL_MSG_ERR, *PNL_MSG_ERR;
BUILD_ASSERT_DECL(sizeof(NL_MSG_ERR)==20);

I think maybe the typedef of struct is constructed at compile time, so assert at compile time is not a bad idea..

posted on 2016-08-30 14:53  三叁  阅读(427)  评论(0编辑  收藏  举报

导航