hangingfan / Technical-Blog

-----------------个人博客-----------------
1 stars 0 forks source link

C++ Static Assertions #12

Open hangingfan opened 6 years ago

hangingfan commented 6 years ago

前置: 预编译阶段有#error可以进行一些判断,例如

#if __STDC__ != 1#  
error "Not a standard compliant compiler"
#endif

但是#error 不能识别 sizeof操作符 image 错误提示: 常量表达式中不允许函数调用

正文: 通过static_assert可以识别sizeof, 但是判断过程是在编译阶段

static_assert(sizeof(long long int) >= 8, "size_t must be at least 64-bits");

区别: image